Hi developers,
I want to write 2 .java
files using JMS
library names are MessageProducer
and MessageConsumer
.
I added activemq-all-5.8.0.jar
and commons-io-2.4.jar
files in my lib
folder.and I changed port number of Activemq
from 61616
to 61617
.
using MessageProducer.java
file,I will send messages to Activemq
.For this I wrote code it's working fine.If you want to see click on this Link.
I want to send messages from Activemq
to MessageConsumer.java
.This is Application is in Apache Tomcat
(http://localhost:8080/ExecutableFileProcess/MessageConsumer
)
Once MessageConsumer
receives the message, it separates the message-body from message and it just print on console(just for my testing).For this I wrote the following 2 java
files.But it's not working.
MessageConsumer.java :
package PackageName;
import java.io.IOException;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.activemq.ActiveMQConnectionFactory;
public class MessageConsumer 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);
Queue queue=session.createQueue("MessageTesing");
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
}
}
}
MyListener.java :
package PackageName;
import javax.jms.Message;
import javax.jms.MessageListener;
public class MyListener implements MessageListener {
public void onMessage(Message msg) {
System.out.println(msg);
}
};
I didn't configure destination for Queue in Activemq console
and also I didn't mention "destination" while sending message from MessageProducer.java
.
I am using Eclipse.How can I print messagebody in console,Actually based on messagebody I will do some operations in my MessageConsumer.java
.but for my testing I need to see messagebody.
I hope,you understand what I am trying.
I am new to JMS
and Java
,so can you explain clearly.So far I wrote the code using Google search.But I didn't find anywhere this issue.
can anyone suggest me.
Thanks.