-
Notifications
You must be signed in to change notification settings - Fork 1
/
TrainMsgProducer.java
78 lines (57 loc) · 2.18 KB
/
TrainMsgProducer.java
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
import java.util.*;
import javax.jms.*;
public class TrainMsgProducer {
public static final int QUEUE = 1;
public static final int TOPIC = 2;
private String serverUrl;
private String name;
private int producerType;
private Connection connection;
private Session session;
private MessageProducer msgProducer;
private Destination destination;
public TrainMsgProducer(String serverUrl, String name, int producerType) {
this.serverUrl = serverUrl;
this.name = name;
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);
} else {
destination = session.createQueue(name);
}
/* create the producer */
msgProducer = session.createProducer(null);
connection.start();
/* create text message */
msg = session.createTextMessage();
/* set message text */
msg.setText((String) message);
/* publish message */
msgProducer.send(destination, msg);
/* close the connection */
connection.close();
} catch (JMSException e) {
e.printStackTrace();
System.exit(-1);
}
}
}