-
Notifications
You must be signed in to change notification settings - Fork 0
/
Consumer.java
103 lines (83 loc) · 3.23 KB
/
Consumer.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
package com.queue;
import java.util.Enumeration;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class Consumer {
private static String subject = "myQueue";
public static void test(){
Session session = null;
Destination destination = null;
try {
final ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_BROKER_URL);
final Connection connection = connectionFactory .createConnection();
System.out.println("Connection : "+connection);
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue(subject);
/* new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Thread started!");
try {
Connection connection = connectionFactory .createConnection();
Session threadSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = threadSession.createConsumer(threadSession.createQueue(subject));
MessageListener listener = new MessageListener() {
@Override
public void onMessage(Message arg0) {
try {
System.out.println("Message recevied! ML ->"+((TextMessage) arg0).getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
};
consumer.setMessageListener(listener);
} catch (JMSException e) {
e.printStackTrace();
}
}
}).start();*/
MessageConsumer consumer = session.createConsumer(destination);
System.out.println("In Consumer...");
TextMessage msg =null;
//Verifying the status of the queue i.e, the messages present in the queue at this moment.
/* QueueBrowser browser = session.createBrowser((Queue) destination);
System.out.println("Looking into queue...");
Enumeration enumeration = browser.getEnumeration();
while(enumeration.hasMoreElements()){
System.out.println("-> "+((TextMessage)enumeration.nextElement()).getText());
}
System.out.println("No more elements...");*/
while((msg = (TextMessage) consumer.receiveNoWait())!=null){
System.out.println("Message recevied! "+msg.getText());
}
//This is to check the messages in the queue after consumption.
/* enumeration = browser.getEnumeration();
System.out.println("Looking into queue...");
while(enumeration.hasMoreElements()){
System.out.println("-> "+((TextMessage)enumeration.nextElement()).getText());
}
System.out.println("No more elements...");
browser.close();*/
// consumer.setMessageListener(this);
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
test();
}
}