-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPubSub.java
181 lines (154 loc) · 5.68 KB
/
PubSub.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package com.topic;
import java.util.Date;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class PubSub {
static ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_BROKER_URL);
static Connection connection = null;
public static void main(String[] args) {
PubSub publisher = new PubSub();
publisher.produce();
}
private void produce() {
System.out.println("In produce() ->");
try {
connection = connectionFactory.createConnection();
connection.setClientID("UnqClntID1");
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination dest = session.createTopic("MyTopic");
MessageProducer messageProducer = session.createProducer(dest);
//filtering using property!
TextMessage message = session.createTextMessage("Hi! intValue = 2"+new Date());
message.setIntProperty("intValue", 2);
message.setJMSCorrelationID(message.getJMSMessageID());
messageProducer.setDeliveryMode(DeliveryMode.PERSISTENT);
TextMessage message1 = session.createTextMessage("Hi! intValue = 1"+new Date());
message1.setIntProperty("intValue", 1);
message1.setJMSCorrelationID(message1.getJMSMessageID());
messageProducer.setDeliveryMode(DeliveryMode.PERSISTENT);
TextMessage testMessage = session.createTextMessage("Hi! for DS's"+new Date());
testMessage.setJMSCorrelationID(testMessage.getJMSMessageID());
messageProducer.setDeliveryMode(DeliveryMode.PERSISTENT);
MessageConsumer consumer1 = session.createConsumer(dest,("intValue=1"));
System.out.println("Consumer1 created");
MessageConsumer consumer2 = session.createConsumer(dest,("intValue=2"));
System.out.println("Consumer2 created");
MessageConsumer consumer3 = session.createDurableSubscriber((Topic) dest,"Subscriber1","(mapMessage=true)",false);
System.out.println("Consumer3 created");
MessageConsumer consumer4 = session.createConsumer(dest,("intValue=1"));
System.out.println("Consumer4 created");
messageProducer.send(message);
messageProducer.send(message1);
messageProducer.send(testMessage);
testMapMessage(session,dest);
System.out.println("Sent!");
connection.start();
while (true) {
message1 = (TextMessage) consumer1.receiveNoWait();
if (message1 != null) {
System.out.println("Message1 received from consumer 1"
+ message1.getText() + message1.getJMSMessageID());
}
else{
break;
}
}
while (true) {
message1 = (TextMessage) consumer4.receiveNoWait();
if (message1 != null) {
System.out.println("Message1 received from consumer 4"
+ message1.getText() + message1.getJMSMessageID());
}
else{
break;
}
}
while (true) {
TextMessage message2 = (TextMessage) consumer2.receiveNoWait();
if (message2 != null) {
System.out.println("Message2 received from consumer 2"
+ message2.getText() + message2.getJMSMessageID());
} else {
break;
}
}
while (true) {
Message message2 = consumer3.receiveNoWait();
if (message2 != null) {
if(message2 instanceof MapMessage){
System.out.println("Message2 received from consumer 2"
+ ((MapMessage) message2).getInt("MIN_VALUE"));
}
else if(message2 instanceof TextMessage){
System.out.println("Message2 received from consumer 2"
+ ((TextMessage) message2).getText() + message2.getJMSMessageID());
}
} else {
break;
}
}
consumer3.close();
durableSubscriber(session,dest);
/*System.out.println("Durable Consumer created");*/
new DurableSubscriber().createDurableSubscriber(dest, connection);
connection.close();
System.out.println("Connection closed!");
} catch (JMSException e) {
e.printStackTrace();
}
}
private void testMapMessage(Session session, Destination dest) {
MapMessage mapMessage = null;
MessageProducer mP=null;
System.out.println("Consumer3 created");
try {
mapMessage = session.createMapMessage();
mapMessage.setBooleanProperty("mapMessage", true);
mapMessage.setInt("MIN_VALUE", Integer.MIN_VALUE);
mP = session.createProducer(dest);
mP.send(mapMessage);
} catch (JMSException e) {
e.printStackTrace();
}
}
private void durableSubscriber(Session session, Destination dest) {
try {
System.out.println("In ds()");
MessageConsumer dconsumer = session.createDurableSubscriber((Topic) dest, "Subscriber2","(mapMessage=true)",false);
System.out.println("Durable Subscriber created");
while (true) {
Message message3 = dconsumer.receiveNoWait() ;
if (message3 != null) {
if (message3 instanceof TextMessage) {
System.out.println("Message3 received "
+ ((TextMessage) message3).getText()
+ message3.getJMSMessageID());
}
else if(message3 instanceof MapMessage){
System.out.println("Message3 received "
+ ((MapMessage) message3).getInt("MIN_VALUE")
+ message3.getJMSMessageID());
}
} else {
System.out.println("Message is null");
break;
}
}
dconsumer.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}