You are not logged in.

  • Login

Dear visitor, welcome to Coder Forum. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Thursday, April 2nd 2009, 9:08pm

Problem mit javax.jms

Guten Abend liebe Community,

ich wurde damit konfrontiert einen Java Messaging Service zu implementieren. Keine Angst, es ist nur eine Aufgabe für die Hochschule ;)
Wir haben dazu ein paar Code-Fetzen bekommen. Ich habe nun Java Message Queue 4.3 installiert und einen Broker angelegt. Ich versuche nun mit einem kleinen Producer-Programm eine Nachricht hineinzuschreiben. Jedoch weiß ich nicht, welche Eigenschaften ich für den InitialContext angeben muss. Ich konnte auch in der Doku nicht besonders viel finden.
Vielleicht hat hier jemand eine Idee?

Java Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import javax.jms.*;
import javax.naming.*;
import java.util.Properties;
 
public class Producer {
 
    private QueueConnectionFactory qFactory = null;
    private QueueConnection qConnect = null;
    private QueueSession qSession = null;
    private Queue sQueue = null;
    private QueueSender qSender = null;
    private String username = null;
    private String password = null;
 
    /* Constructor. Establish the Producer */
    public Producer(String broker, String username, String password) throws Exception {
        // Obtain a JNDI connection
        Properties env = new Properties();
        env.put("java.naming.factory.initial","com.sun.jndi.fscontext.RefFSContextFactory");
        env.put("java.naming.provider.url", "localhost:7676");
        env.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
        // ... specify the JNDI properties sprecific to the
        //     provider
        InitialContext jndi = new InitialContext(env);
        this.username = username;
        this.password = password;
 
        // Look up a JMS QueueConnectionFactory
        qFactory = (QueueConnectionFactory) jndi.lookup(broker);
 
        // Create a JMS QueueConnection object
        qConnect = qFactory.createQueueConnection(username, password);
 
        // Create one JMS QueueSession object
        qSession = qConnect.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
 
        // Look up for a JMS Queue hello
        sQueue = (Queue) jndi.lookup("hello");
 
        // Create a sender
        qSender = qSession.createSender(sQueue);
 
        // Start the Connection
        qConnect.start();
    }
 
    /* Create and send message using qSender */
    protected void sendMessage() throws JMSException {
        // Create message
        TextMessage message = qSession.createTextMessage();
        // Set payload
        message.setText(username + " Hello");
        // Send Message
        qSender.send(message);
    }
 
    /* Close the JMS connection */
    public void close() throws JMSException {
        qConnect.close();
    }
 
    /* Run the Producer */
    public static void main(String args[]) {
        String broker, username, password;
        if (args.length == 3) {
            broker = args[0];
            username = args[1];
            password = args[2];
        } else {
            System.out.println("Bitte Parameter angeben (broker username password)");
            return;
        }
        // Create Producer
        try {
            System.out.println("Hier bin ich schonma");
            Producer producer = new Producer(broker, username, password);
            System.out.println("hier auch");
            // Send the message
            producer.sendMessage();
            System.out.println("so, jetzt Nachricht gesendet");
            // Close connection
            producer.close();
        } catch (Exception e) {
            System.out.println("Fehler: "+e);
        }
    }
}


Danke im Voraus!

2

Wednesday, April 8th 2009, 7:02pm

Um das ganze einmal aufzulösen, falls noch jemand auf dieses Problem stößt.

Schrittfolge:
1. OpenMQ
1.1. Broker anlegen
1.2. ObjectStroe anlgen
1.3. ConnectionFactory anlegen
1.4 Target anlegen
2. mit Java auf genau diese Objekte zugreifen

Ein Beispiel Quelltext:

Java Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import javax.jms.*;
import javax.naming.*;
import java.util.Properties;
 
public class Producer {
 
    private QueueConnectionFactory qFactory = null;
    private QueueConnection qConnect = null;
    private QueueSession qSession = null;
    private Queue sQueue = null;
    private QueueSender qSender = null;
 
    private String broker = null;
    private String username = null;
    private String password = null;
 
    /* Run the Producer */
    public static void main(String args[]) {
        String broker, username, password;
        if (args.length == 3) {
            broker = args[0];
            username = args[1];
            password = args[2];
        } else {
            broker = "MyQueueConnectionFactory";
            username = "admin";
            password = "admin";
 
            System.out.println("Nächstes Mal bitte Parameter angeben (broker username password)");
            System.out.println("Broker: " + broker);
            System.out.println("Username: " + username);
            System.out.println("Password: " + password);
 
        }
        // Create Producer
        try {
            System.out.println("Hier bin ich schonma");
            Producer producer = new Producer(broker, username, password);
            System.out.println("hier auch");
            // Send the message
            producer.sendMessage();
            System.out.println("so, jetzt Nachricht gesendet");
            // Close connection
            producer.close();
        } catch (Exception e) {
            System.out.println("Fehler: "+e);
        }
    }
 
 
    /* Constructor. Establish the Producer */
    public Producer(String broker, String username, String password) throws Exception {
 
        Properties env = new Properties();
 
        // JNDI-Properties
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
        env.put(Context.PROVIDER_URL, "file:///C:/Temp");
 
        // Netzwerkverbindung zum Namensserver
        InitialContext jndi = new InitialContext(env);
        //InitialContext jndi = new InitialContext();
 
        this.username = username;
        this.password = password;
 
        // Look up a JMS QueueConnectionFactory
        qFactory = (QueueConnectionFactory) jndi.lookup(broker);
 
        // Create a JMS QueueConnection object
        qConnect = qFactory.createQueueConnection(username, password);
 
        // Create one JMS QueueSession object
        qSession = qConnect.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
 
        // Look up for a JMS Queue hello
        sQueue = (Queue) jndi.lookup("MyQueue");
 
        // Create a sender
        qSender = qSession.createSender(sQueue);
 
        // Start the Connection
        qConnect.start();
    }
 
    /* Create and send message using qSender */
    protected void sendMessage() throws JMSException {
        // Create message
        TextMessage message = qSession.createTextMessage();
        // Set payload
        message.setText(username + " Hello");
        // Send Message
        qSender.send(message);
    }
 
    /* Close the JMS connection */
    public void close() throws JMSException {
        qConnect.close();
    }
}


Mehr Infos dazu direkt bei Java MQ: http://docs.sun.com/app/docs/coll/1307.6 (Achtung! In der PDF-Version des Guides gibt es detailliertere Informationen als in der HTML-Version)

Social bookmarks