如何将消息从Activemq推送到Java应用程序
嗨,开发者们 我想用如何将消息从Activemq推送到Java应用程序,java,jms,activemq,java-ee-6,Java,Jms,Activemq,Java Ee 6,嗨,开发者们 我想用JMS编写2个.java文件,库名是MessageProducer和MessageConsumer 我在我的lib文件夹中添加了activemq-all-5.8.0.jar和commons-io-2.4.jar文件,并将activemq的端口号从6161616更改为6161617 使用MessageProducer.java文件,我将向Activemq发送消息。为此,我编写了代码,工作正常。如果您想查看,请单击此 我想将消息从Activemq发送到MessageConsum
JMS
编写2个.java
文件,库名是MessageProducer
和MessageConsumer
我在我的lib
文件夹中添加了activemq-all-5.8.0.jar
和commons-io-2.4.jar
文件,并将activemq
的端口号从6161616
更改为6161617
使用MessageProducer.java
文件,我将向Activemq
发送消息。为此,我编写了代码,工作正常。如果您想查看,请单击此
我想将消息从Activemq
发送到MessageConsumer.java
。这是应用程序位于apachetomcat
(http://localhost:8080/ExecutableFileProcess/MessageConsumer
)
一旦MessageConsumer
接收到消息,它就将消息体与消息分离,并在控制台上打印(仅用于我的测试)。为此,我编写了以下2个java
文件。但它不起作用
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);
}
};
我没有在Activemq控制台中为队列配置目的地,而且在从MessageProducer.java
发送消息时也没有提到“目的地”
我正在使用Eclipse。如何在控制台中打印messagebody,实际上是基于messagebody,我将在我的MessageConsumer.java
中执行一些操作。但是为了进行测试,我需要查看messagebody
我希望你能理解我的想法
我不熟悉JMS
和Java
,所以你能解释清楚吗。到目前为止,我是用谷歌搜索编写代码的。但是我没有发现这个问题
有人能推荐我吗
谢谢。您的程序可能只是启动和终止,因为您已经编写了客户端来异步接收消息(通过使用MessageListener
,在不同的线程中),而您的主线程刚刚终止
试着这样做:
connection.start();
System.out.println(“按键终止”);
试一试{
System.in.read();
}捕获(IOE异常){
e、 printStackTrace();
}
系统输出打印项次(“结束”);
这会让你的程序一直运行,直到你按下一个键
要阅读文本消息的正文
,请使用以下内容(引自:
上面是一个实用程序类
创建连接
创建/连接到队列
发送消息
接收消息
如果我运行任何程序,我将收到3条警告消息。此链接中的所有内容均已检查
http://stackoverflow.com/questions/18129668/how-can-i-remove-warning-messages-in-activemq
。我在我的程序中添加了你的代码,第一次运行良好。第二次尝试时,它不工作。我想是因为这个警告。如何修复此问题。当程序在第一次运行期间收到消息时,这些消息已被使用(之后队列为空)。您必须发送一些新消息。警告消息与log4j相关,这应该不是问题。刚才我明白了,但是如何修复log4j
file@user2642355我来看看另一个问题。但是这个问题你需要更多的帮助吗?是的,我想从消息中获取消息正文。我没有找到任何方法。我尝试了System.out.println(msg.getJMSMessageID());
。但我正在获取messageId。如果您知道有任何函数可以从消息中获取messagebody。嗨,亲爱的,请看这个问题。我非常需要这个。
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
String text = textMessage.getText();
System.out.println("Received: " + text);
} else {
System.out.println("Received: " + message);
}
import java.io.Serializable;
import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.log4j.Logger;
public class ActiveMQConnection {
private static final long timeout = 3000L;
private Logger logger = Logger.getLogger(ActiveMQConnection.class);
private String activeMQUser;
private String activeMQPassword;
private String activeMQURI;
private String activeMQQueueName;
private ActiveMQConnectionFactory connectionFactory;
private Connection connection;
private Session session;
private boolean isConnected;
private boolean transacted = false;
private Destination destinationQueue;
private boolean isQueueAvailable;
private boolean isProducerAvailable;
private boolean isConsumerAvailable;
private MessageProducer producerForQueue;
private MessageConsumer consumerForQueue;
/**
* Create Default service
* @throws Exception
*/
public ActiveMQConnection() throws Exception
{
try {
throw new JMSException("No Parameters defined for creating connection. Try constructor with parameters.");
} catch (JMSException e) {
logger.error("JMS Exception in Active MQ Connection",e);
throw e;
} catch (Exception e) {
logger.error("JMS Exception in Active MQ Connection",e);
throw e;
}
}
/**
* Create a service with desired parameters.
* @param activeMQUser
* @param activeMQPassword
* @param activeMQURI
* @param activeMQQueueName
* @throws Exception
*/
public ActiveMQConnection(String activeMQUser, String activeMQPassword, String activeMQURI) throws Exception
{
try {
this.activeMQUser = activeMQUser;
this.activeMQPassword = activeMQPassword;
this.activeMQURI = activeMQURI;
setUpActiveMQConnection();
} catch (JMSException e) {
logger.error("JMS Exception in Active MQ Connection",e);
throw e;
} catch (Exception e) {
logger.error("Exception in Active MQ Connection",e);
throw e;
}
}
/**
* @throws JMSException, Exception
*/
private void setUpActiveMQConnection() throws JMSException, Exception
{
connectionFactory = new ActiveMQConnectionFactory(
this.activeMQUser,
this.activeMQPassword,
this.activeMQURI );
try {
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);
isConnected = true;
}catch (JMSException e) {
isConnected = false;
throw e;
}catch(Exception e){
isConnected = false;
throw e;
}
}
/**
* @throws Exception
*/
public void setUpQueue(String queueName ) throws Exception
{
this.activeMQQueueName = queueName;
createQueue();
createProducer();
createConsumer();
}
/**
* @throws Exception
*/
private void createQueue() throws Exception
{
try {
if(destinationQueue == null)
{
destinationQueue = session.createQueue(this.activeMQQueueName);
isQueueAvailable = true;
}
} catch (JMSException e) {
isQueueAvailable = false;
throw e;
}catch(Exception e){
isQueueAvailable = false;
throw e;
}
}
/**
* @throws JMSException
*
*/
private void createProducer() throws JMSException
{
if(producerForQueue == null)
{
try {
producerForQueue = session.createProducer(destinationQueue);
producerForQueue.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
isProducerAvailable = true;
} catch (JMSException e) {
isProducerAvailable = false;
throw e;
}
}
}
/**
* @throws JMSException
*/
private void createConsumer() throws JMSException
{
if(consumerForQueue == null)
{
try {
consumerForQueue = session.createConsumer(destinationQueue);
isConsumerAvailable = true;
} catch (JMSException e) {
isConsumerAvailable = false;
throw e;
}
}
}
/**
* @param objectToQueue
* @throws JMSException
*/
public void sendMessage(Serializable objectToQueue) throws JMSException
{
ObjectMessage message = session.createObjectMessage();
message.setObject(objectToQueue);
producerForQueue.send(message);
}
/**
* @param objectToQueue
* @throws JMSException
*/
public Serializable receiveMessage() throws JMSException
{
Message message = consumerForQueue.receive(timeout);
if (message instanceof ObjectMessage)
{
ObjectMessage objMsg = (ObjectMessage) message;
Serializable sobject = objMsg.getObject();
return sobject;
}
return null;
}
/**
* close-MQ-Connection
*/
public void closeMQConnection()
{
try
{
if(consumerForQueue != null)
{
consumerForQueue.close();
}
if(producerForQueue != null)
{
producerForQueue.close();
}
if(session != null)
{
session.close();
}
if(connection != null )
{
connection.close();
}
}
catch (JMSException e)
{
logger.info("Error while closing connection.",e);
}
finally
{
consumerForQueue = null;
producerForQueue = null;
destinationQueue = null;
session = null;
connection = null;
activeMQUser = null;
activeMQPassword = null;
activeMQQueueName = null;
activeMQURI = null;
}
}
public boolean isConnected() {
return isConnected;
}
public void setConnected(boolean isConnected) {
this.isConnected = isConnected;
}
public boolean isQueueAvailable() {
return isQueueAvailable;
}
public void setQueueAvailable(boolean isQueueAvailable) {
this.isQueueAvailable = isQueueAvailable;
}
public boolean isProducerAvailable() {
return isProducerAvailable;
}
public void setProducerAvailable(boolean isProducerAvailable) {
this.isProducerAvailable = isProducerAvailable;
}
public MessageConsumer getConsumerForQueue() {
return consumerForQueue;
}
public void setConsumerForQueue(MessageConsumer consumerForQueue) {
this.consumerForQueue = consumerForQueue;
}
public boolean isConsumerAvailable() {
return isConsumerAvailable;
}
public void setConsumerAvailable(boolean isConsumerAvailable) {
this.isConsumerAvailable = isConsumerAvailable;
}
}