Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I subscribe Java program as a consumer in Activemq?

Tags:

java

jms

activemq

I want to implement Pub/Sub domain in project.Basically I am not a java developer,using google help.I read this Link. I started to implement following structure. enter image description here

I wrote Java Application name as MessageConsumer.java for receiving messages from AMQ broker and placed in Webserver(Apache Tomcat).

MessageConsumercode:

 package PackageName;
 import java.io.IOException;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.jms.*;
 import org.apache.activemq.ActiveMQConnectionFactory;
 public class Consumer extends HttpServlet {
 @Override
 protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
    throws ServletException, IOException {
try {
//creating connectionfactory object for way
ConnectionFactory connectionFactory=new ActiveMQConnectionFactory("admin","admin","tcp://localhost:61617");
//establishing the connection b/w this Application and Activemq
Connection connection=connectionFactory.createConnection();
Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic queue=session.createTopic("MessageTesting");
javax.jms.MessageConsumer consumer=session.createConsumer(queue);
//fetching queues from Activemq
MessageListener listener = new MyListener();
consumer.setMessageListener(listener);
connection.start();
}
catch (Exception e) {
// TODO: handle exception
}
}

}

Separatly I wrote another Java Application for processing messages(MyListener.java).

MyListener.java code :

package PackageName;
import java.io.*;
import java.net.*;
import javax.jms.*;
public class MyListener implements MessageListener {
public void onMessage(Message msg) {
    TextMessage msg1=(TextMessage)msg;
    //just for your understanding I mention dummy code
    //System.out.println(msg1.getText());
    MyListener ml=new MyListener();
    try {

      ml.executeHttp("http://localhost:8080/ExecutableFileProcess/ClassName");
        System.out.println(msg1.getText());
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}}

Both Java Applications are in webserver(Apache Tomcat).so far we are following in the following way.

  1. Before sending messages to Topic,we are triggering MessageConsumer.java through HTTP on browser.

Right know, what we are trying.Initially we don't want to trigger MessageConsumer.java.

Means,Assume MessageConsumer.java is in Webserver. Initially If AMQ get message from anywhere, our MessageConsumer.java should be process their own logic.

I hope, you guys understand what We are trying.

I never work on Apache Camel, can you explain clearly.

Thanks.

like image 279
Hanumath Avatar asked Sep 07 '13 10:09

Hanumath


People also ask

How do I subscribe to a topic in ActiveMQ?

Subscribers can subscribe to a topic on ActiveMQ as durable subscribers by using the createDurableSubscriber() method available in javax. jms. Session class. To create a non-durable subscriber, instead of using the session.

What is consumer in ActiveMQ?

Consumers. Consumers are the applications that receive the messages ActiveMQ sends.

Does ActiveMQ support Java?

ActiveMQ 5.0-5.7 supports any Java platform of Java 5.0 or later.


2 Answers

why you want to trigger MessageConsumer.java manually as invocation of Subscriber is the resposibility of ActiveMQ in your case.

From your topic publish your message to ActiveMQ server and all the subscribers who subscribed to that topic will get your message without manually triggering it.

refer this as your initial POC http://activemq.apache.org/hello-world.html.

You can use below java code for subscribing for topic for client2 and client3

import javax.jms.*;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class consumer {
    // URL of the JMS server
    private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;

    // Name of the topic from which we will receive messages from = " testt"

    public static void main(String[] args) throws JMSException {
        // Getting JMS connection from the server

        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        Connection connection = connectionFactory.createConnection();
        connection.start();

        Session session = connection.createSession(false,
                Session.AUTO_ACKNOWLEDGE);

        Topic topic = session.createTopic("testt");

        MessageConsumer consumer = session.createConsumer(topic);

        MessageListener listner = new MessageListener() {
            public void onMessage(Message message) {
                try {
                    if (message instanceof TextMessage) {
                        TextMessage textMessage = (TextMessage) message;
                        System.out.println("Received message"
                                + textMessage.getText() + "'");
                    }
                } catch (JMSException e) {
                    System.out.println("Caught:" + e);
                    e.printStackTrace();
                }
            }
        };
        consumer.setMessageListener(listner);

        try {
              System.in.read();
         } catch (IOException e) {
             e.printStackTrace();
         }
    connection.close();

}
}    
like image 144
Alpesh Gediya Avatar answered Nov 15 '22 03:11

Alpesh Gediya


Did you check Apache Camel? http://camel.apache.org/

You can define routes with camel to publish and subscribe to topics on a broker from java code (integration with spring beans for instance). There are many examples including interaction with an activemq message broker.

like image 33
zenbeni Avatar answered Nov 15 '22 02:11

zenbeni