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!