How can I subscribe Java program as a consumer in 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.
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.
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.