-
Notifications
You must be signed in to change notification settings - Fork 0
/
LocalJMS.java
53 lines (44 loc) · 1.57 KB
/
LocalJMS.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
package com.topic;
import java.util.Date;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
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 LocalJMS {
static ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_BROKER_URL);
public static void main(String[] args) {
try {
Connection con = connectionFactory.createConnection();
Session session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic("myTopic");
MessageProducer mP = session.createProducer(topic);
final MessageConsumer consumer = session.createConsumer(topic);
mP.send(session.createTextMessage("Hi "+new Date()));
System.out.println("Message sent!");
/*consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message arg0) {
System.out.println("In onMessage() -> ");
try {
System.out.println(((TextMessage) arg0).getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
});*/
con.start();
final TextMessage message = (TextMessage) consumer.receive();
System.out.println(message.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}