Wednesday, April 16, 2014

How to write subscriber and publisher to JBOSS MQ topic

This Post explains Topics in JBOSS MQ with Subscribing and Publishing. For this we will write two java clients.

  • TopicSubscriber.java to Subcribe for messages
  • TopicPublisher.java to to Publish the messages
First you have to download JBOSS Application Server from here. In this sample I'm using jboss-4.2.2.GA. Before starting the JBOSS application server you have to create a topic in JBOSS server. To do that, you have to create myTopoc-service.xml (you can used what every name you want) under the <JBOSS_SERVER>/server/default/deploy and enter following xml into it.


1
2
3
<mbean code="org.jboss.mq.server.jmx.Topic" name="jboss.mq.destination:service=Queue,name=topicA">
        <depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
</mbean>

After that you can start the JBOSS application server. From the console log you can verify the topicA was created.

Now you can create the sample project on IDE that you preferred. Also make sure to add client and lib directory jars in the JBOSS application server to the project. Now you can create TopicSubscriber.java and TopicPublisher.java sample programs as follows.

TopicSubscriber.java sample program

 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
package simple;

import java.util.Properties;

import javax.jms.*;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class TopicSubscriber {

    private String topicName = "topic/topicA";

    private boolean messageReceived = false;

    private static javax.naming.Context mContext = null;
    private static TopicConnectionFactory mTopicConnectionFactory = null;
    private TopicConnection  topicConnection = null;

    public static void main(String[] args) {
        TopicSubscriber subscriber = new TopicSubscriber();
        subscriber.subscribeWithTopicLookup();
    }

    public void subscribeWithTopicLookup() {

        Properties properties = new Properties();
        properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
        properties.put(Context.PROVIDER_URL, "jnp://localhost:1099");
        properties.put("topic." + topicName, topicName);

        try {

            mContext = new InitialContext(properties);
            mTopicConnectionFactory = (TopicConnectionFactory)mContext.lookup("ConnectionFactory");

            topicConnection = mTopicConnectionFactory.createTopicConnection();

            System.out.println("Create Topic Connection for Topic " + topicName);

            while (!messageReceived) {
                try {
                    TopicSession topicSession = topicConnection
                            .createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

                    Topic topic = (Topic) mContext.lookup(topicName);
                    // start the connection
                    topicConnection.start();

                    // create a topic subscriber
                    javax.jms.TopicSubscriber topicSubscriber = topicSession.createSubscriber(topic);

                    TestMessageListener messageListener = new TestMessageListener();
                    topicSubscriber.setMessageListener(messageListener);

                    Thread.sleep(5000);
                    topicSubscriber.close();
                    topicSession.close();
                } catch (JMSException e) {
                    e.printStackTrace();
                } catch (NamingException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        } catch (NamingException e) {
            throw new RuntimeException("Error in initial context lookup", e);
        } catch (JMSException e) {
            throw new RuntimeException("Error in JMS operations", e);
        } finally {
            if (topicConnection != null) {
                try {
                    topicConnection.close();
                } catch (JMSException e) {
                    throw new RuntimeException(
                            "Error in closing topic connection", e);
                }
            }
        }
    }

    public class TestMessageListener implements MessageListener {
        public void onMessage(Message message) {
            try {
                System.out.println("Got the Message : "
                        + ((TextMessage) message).getText());
                messageReceived = true;
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    }

}

TopicPublisher.java sample program

 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
package simple;

import javax.jms.*;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Properties;

public class TopicPublisher {
    private String topicName = "topic/topicA";

    private static javax.naming.Context mContext = null;
    private static TopicConnectionFactory mTopicConnectionFactory = null;

    public static void main(String[] args) {
        TopicPublisher publisher = new TopicPublisher();
        publisher.publishWithTopicLookup();
    }

    public void publishWithTopicLookup() {
        Properties properties = new Properties();
        TopicConnection topicConnection = null;
        properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
        properties.put(Context.PROVIDER_URL, "jnp://localhost:1099");
        properties.put("topic." + topicName, topicName);

        try {

            mContext = new InitialContext(properties);
            mTopicConnectionFactory = (TopicConnectionFactory)mContext.lookup("ConnectionFactory");
            topicConnection = mTopicConnectionFactory.createTopicConnection();

            try {
                TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

                // create or use the topic
                System.out.println("Use the Topic " + topicName);
                Topic topic = (Topic) mContext.lookup(topicName);

                javax.jms.TopicPublisher topicPublisher = topicSession.createPublisher(topic);

                String msg = "Hi, I am Test Message";
                TextMessage textMessage = topicSession.createTextMessage(msg);

                topicPublisher.publish(textMessage);
                System.out.println("Publishing message " + textMessage);

                topicPublisher.close();
                topicSession.close();

                Thread.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        } catch (JMSException e) {
            throw new RuntimeException("Error in JMS operations", e);
        } catch (NamingException e) {
            throw new RuntimeException("Error in initial context lookup", e);
        }
    }

}u

You have to used PROVIDER_URL as "java.naming.provider.url" and INITIAL_CONTEXT_FACTORY as "java.naming.factory.initial".

First you have to run TopicSubscriber and then run the TopicPublisher. Here are the output of them.

TopicSubscriber;
Create Topic Connection for Topic topic/topicA
Got the Message : Hi, I am Test Message

TopicPublisher;
Use the Topic topic/topicA
Publishing message SpyTextMessage {
Header {
   jmsDestination  : TOPIC.topicA
   jmsDeliveryMode : 2
   jmsExpiration   : 0
   jmsPriority     : 4
   jmsMessageID    : ID:2-13977171929621
   jmsTimeStamp    : 1397717192962
   jmsCorrelationID: null
   jmsReplyTo      : null
   jmsType         : null
   jmsRedelivered  : false
   jmsProperties   : {}
   jmsPropReadWrite: true
   msgReadOnly     : false
   producerClientId: ID:2
}
Body {
   text            :Hi, I am Test Message
}

}


Java Code Geeks

Wednesday, April 9, 2014

How to run Apache Stratos and subscribe to cartridges and how autoscaling is happen

In this video I'm going to show,


  • How do I run Stratos on my laptop
  • PaaS story video on how to get it done
  • How to deploy apps - PHP and Tomcat 
  • JMeter to load up and instances popping up