/
NormalConsumerProblem.java
54 lines (47 loc) · 1.56 KB
/
NormalConsumerProblem.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
package lab03.message.pubsub;
import labxx.common.settings.CommonSettings;
import javax.jms.*;
public class NormalConsumerProblem {
private static ConnectionFactory connectionFactory = null;
private static Topic defaultTopic = null;
static {
connectionFactory = CommonSettings.getConnectionFactory();
defaultTopic = CommonSettings.getDefautTopic();
}
public static void main(String[] args) {
Thread publisher = new Thread() {
@Override
public void run() {
try (JMSContext jmsContext = connectionFactory.createContext()) {
JMSProducer producer = jmsContext.createProducer();
Thread.sleep(1000);
for (int i = 1; i < 7; i++) {
producer.send(defaultTopic, "Update " + i);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
//Normal Consumer
Thread consumer = new Thread() {
@Override
public void run() {
try (JMSContext jmsContext = connectionFactory.createContext()) {
JMSConsumer consumer = jmsContext.createConsumer(defaultTopic);
System.out.println(consumer.receive().getBody(String.class));
Thread.sleep(2000);
consumer.close();
consumer = jmsContext.createConsumer(defaultTopic);
for (int i = 1; i < 6; i++) {
System.out.println(consumer.receive().getBody(String.class));
}
} catch (JMSException | InterruptedException e) {
e.printStackTrace();
}
}
};
publisher.start();
consumer.start();
}
}