-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTrainMsgSyncProducer.java
More file actions
111 lines (90 loc) · 4.01 KB
/
TrainMsgSyncProducer.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
import java.util.*;
import javax.jms.*;
public class TrainMsgSyncProducer {
public static final int QUEUE = 1;
public static final int TOPIC = 2;
private String serverUrl;
private String name;
private String replyName;
private int producerType;
private Connection connection;
private Session session;
private MessageProducer msgProducer;
private MessageConsumer msgConsumer;
private Destination destination;
private Destination replyDestination;
public TrainMsgSyncProducer(String serverUrl, String name, String replyName, int producerType) {
this.serverUrl = serverUrl;
this.name = name;
this.replyName = replyName;
this.producerType = producerType;
try {
tibjmsUtilities.initSSLParams(serverUrl, new String[0]);
} catch (JMSSecurityException e) {
System.err.println("JMSSecurityException: " + e.getMessage() + ", provider=" + e.getErrorCode());
e.printStackTrace();
System.exit(0);
}
}
public void sendMessage(String message) {
try {
TextMessage msg;
int i;
System.err.println("Publishing to destination '" + name + "'\n");
ConnectionFactory factory = new com.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
Message reply = msgConsumer.receive();
// Process the reply.
printMsg(reply);
/* close the connection */
connection.close();
} catch (JMSException e) {
e.printStackTrace();
System.exit(-1);
}
}
public void printMsg(Message msg) throws JMSException {
if (msg instanceof TextMessage) {
TextMessage replyMessage = (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());
}
}
}