1 /*------------------------------------------------------------------------------
2 Name: SimpleJmsDemo.java
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 ------------------------------------------------------------------------------*/
6 package javaclients.jms;
7
8 import javax.jms.JMSException;
9 import javax.jms.Message;
10 import javax.jms.MessageListener;
11 import javax.jms.Session;
12 import javax.jms.TextMessage;
13 import javax.jms.Topic;
14 import javax.jms.TopicConnection;
15 import javax.jms.TopicConnectionFactory;
16 import javax.jms.TopicPublisher;
17 import javax.jms.TopicSession;
18 import javax.jms.TopicSubscriber;
19
20 import org.xmlBlaster.jms.XBConnectionFactory;
21 import org.xmlBlaster.jms.XBDestination;
22
23 /**
24 * SimpleTest
25 *
26 * @author <a href="mailto:michele@laghi.eu">Michele Laghi</a>
27 *
28 */
29 public class SimpleJmsDemo implements MessageListener {
30
31 private String[] cmdLine;
32
33 public SimpleJmsDemo(String[] cmdLine) {
34 this.cmdLine = cmdLine;
35 }
36
37 public void prepare() throws JMSException {
38 try {
39 // create a factory (normally retreived by naming service)
40 TopicConnectionFactory factory = new XBConnectionFactory(null, this.cmdLine, false);
41 // should be retreived via jndi
42 Topic topic = new XBDestination("jms-test", null, false);
43
44 TopicConnection connection = factory.createTopicConnection();
45 connection.start();
46 TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
47 TopicSubscriber subscriber = session.createSubscriber(topic);
48 subscriber.setMessageListener(this);
49
50 TopicPublisher publisher = session.createPublisher(topic);
51
52 TextMessage msg = session.createTextMessage();
53 msg.setText("this is a simple jms test message");
54 publisher.publish(msg);
55
56 Thread.sleep(3000L);
57 connection.stop();
58 }
59 catch (Exception ex) {
60 ex.printStackTrace();
61 }
62 }
63
64 public void onMessage(Message message) {
65 try {
66 if (message instanceof TextMessage) {
67 System.out.println(((TextMessage)message).getText());
68 }
69 }
70 catch (JMSException ex) {
71 System.err.println(ex.getMessage());
72 ex.printStackTrace();
73 }
74 }
75
76 public static void main(String[] args) {
77 SimpleJmsDemo test = new SimpleJmsDemo(args);
78 try {
79 test.prepare();
80 }
81 catch (Exception ex) {
82 ex.printStackTrace();
83 }
84 }
85 }
syntax highlighted by Code2HTML, v. 0.9.1