- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTrainMsgSyncProducer.java
More file actions
Latest commit
111 lines (90 loc) · 4.01 KB
/
Copy pathTrainMsgSyncProducer.java
File metadata and controls
111 lines (90 loc) · 4.01 KB
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
101
102
103
104
105
106
107
108
109
110
111
importjava.util.*;
importjavax.jms.*;
publicclassTrainMsgSyncProducer {
publicstaticfinalintQUEUE = 1;
publicstaticfinalintTOPIC = 2;
privateStringserverUrl;
privateStringname;
privateStringreplyName;
privateintproducerType;
privateConnectionconnection;
privateSessionsession;
privateMessageProducermsgProducer;
privateMessageConsumermsgConsumer;
privateDestinationdestination;
privateDestinationreplyDestination;
publicTrainMsgSyncProducer(StringserverUrl, Stringname, StringreplyName, intproducerType) {
this.serverUrl = serverUrl;
this.name = name;
this.replyName = replyName;
this.producerType = producerType;
try {
tibjmsUtilities.initSSLParams(serverUrl, newString[0]);
} catch (JMSSecurityExceptione) {
System.err.println("JMSSecurityException: " + e.getMessage() + ", provider=" + e.getErrorCode());
e.printStackTrace();
System.exit(0);
}
}
publicvoidsendMessage(Stringmessage) {
try {
TextMessagemsg;
inti;
System.err.println("Publishing to destination '" + name + "'\n");
ConnectionFactoryfactory = newcom.tibco.tibjms.TibjmsConnectionFactory(serverUrl);
//No username
connection = factory.createConnection("", "");
/* create the session */
session = connection.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
/* create the destination */
if (producerType == TrainMsgProducer.TOPIC) {
destination = session.createTopic(name);
replyDestination = session.createTopic(replyName);
} else {
destination = session.createQueue(name);
replyDestination = session.createQueue(replyName);
}
/* create the producer */
msgProducer = session.createProducer(null);
msgConsumer = session.createConsumer(replyDestination);
connection.start();
/* create text message */
msg = session.createTextMessage();
/* set message text */
msg.setText((String) message);
msg.setJMSReplyTo(replyDestination);
/* publish message */
msgProducer.send(destination, msg);
System.out.println("Message deployed awaiting reply...");
//Wait for a reply regarding Bus Deployed
// Send a request and wait for a reply. Code also can be added to time-out the wait
Messagereply = msgConsumer.receive();
// Process the reply.
printMsg(reply);
/* close the connection */
connection.close();
} catch (JMSExceptione) {
e.printStackTrace();
System.exit(-1);
}
}
publicvoidprintMsg(Messagemsg) throwsJMSException {
if (msginstanceofTextMessage) {
TextMessagereplyMessage = (TextMessage) msg;
System.out.println("Received reply ");
System.out.println("\tTime: " + System.currentTimeMillis() + " ms");
System.out.println("\tMessage ID: " + replyMessage.getJMSMessageID());
System.out.println("\tCorrel. ID: " + replyMessage.getJMSCorrelationID());
System.out.println("\tReply to: " + replyMessage.getJMSReplyTo());
System.out.println("\tContents: " + replyMessage.getText());
} else {
System.out.println("Invalid message detected");
System.out.println("\tType: " + msg.getClass().getName());
System.out.println("\tTime: " + System.currentTimeMillis() + " ms");
System.out.println("\tMessage ID: " + msg.getJMSMessageID());
System.out.println("\tCorrel. ID: " + msg.getJMSCorrelationID());
System.out.println("\tReply to: " + msg.getJMSReplyTo());
msg.setJMSCorrelationID(msg.getJMSMessageID());
}
}
}