-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueueSenderExample.java
140 lines (118 loc) · 3.49 KB
/
QueueSenderExample.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import java.util.Random;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class QueueSenderExample {
public static void main(String [] args) {
String host = "localhost";
GameObtainer sender = null;
try {
sender = new GameObtainer(host);
sender.sendMessages();
} catch (NamingException | JMSException e) {
System.err.println("Program aborted");
} finally {
if(sender != null) {
try {
sender.close();
} catch (Exception e) { }
}
}
}
private String host;
public QueueSenderExample(String host) throws NamingException, JMSException {
this.host = host;
// Access JNDI
createJNDIContext();
// Lookup JMS resources
lookupConnectionFactory();
lookupQueue();
// Create connection->session->sender
createConnection();
}
private Context jndiContext;
private void createJNDIContext() throws NamingException {
try {
System.setProperty("org.omg.CORBA.ORBInitialHost", host);
System.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
jndiContext = new InitialContext();
} catch (NamingException e) {
System.err.println("Could not create JNDI API context: " + e);
throw e;
}
}
private ConnectionFactory connectionFactory;
private void lookupConnectionFactory() throws NamingException {
try {
connectionFactory = (ConnectionFactory)jndiContext.lookup("jms/TestConnectionFactory");
} catch (NamingException e) {
System.err.println("JNDI API JMS connection factory lookup failed: " + e);
throw e;
}
}
private Queue queue;
private void lookupQueue() throws NamingException {
try {
queue = (Queue)jndiContext.lookup("jms/TestQueue");
} catch (NamingException e) {
System.err.println("JNDI API JMS queue lookup failed: " + e);
throw e;
}
}
private Connection connection;
private void createConnection() throws JMSException {
try {
connection = connectionFactory.createConnection();
connection.start();
} catch (JMSException e) {
System.err.println("Failed to create connection to JMS provider: " + e);
throw e;
}
}
public void sendMessages() throws JMSException {
createSession();
createSender();
int count = 1 + new Random().nextInt(10);
TextMessage message = session.createTextMessage();
for(int i=1; i<=count; ++i) {
message.setText("This is message "+i);
queueSender.send(message);
System.out.println("Sending message "+i);
}
// send non-text control message to end
queueSender.send(session.createMessage());
}
private Session session;
private void createSession() throws JMSException {
try {
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
} catch (JMSException e) {
System.err.println("Failed to create session: " + e);
throw e;
}
}
// Was: QueueSender
private MessageProducer queueSender;
private void createSender() throws JMSException {
try {
queueSender = session.createProducer(queue);
} catch (JMSException e) {
System.err.println("Failed to create session: " + e);
throw e;
}
}
public void close() {
if(connection != null) {
try {
connection.close();
} catch (JMSException e) { }
}
}
}