java - 无法从jms队列接收消息

标签 java queue jms

我是 jms 的初学者,我只是想运行一个简单的 Web 应用程序来生成和使用消息。因此,在我的欢迎页面上,我有一个带有文本框和提交按钮的表单。提交时,文本框中的文本将包装在消息中,并通过 MessageProducer 发送到服务器上的队列。据我了解,此消息应保留在队列中,直到我从消费者链接到该队列的某个 MessageConsumer 调用接收方法。不幸的是,在我单击“获取消息”按钮的第二个 jsp 页面上,收到的消息显示为空。我很确定它们已正确发送(至少没有错误说明),但有什么方法可以检查吗?关于出了什么问题有什么想法吗?提前致谢。这是生产者/消费者代码。

public class Producer {           
    private static final String CONNECTION_FACTORY = "connectionFactory";     
    private static final String CONNECTION_QUEUE = "jms/myqueue";    
    private static final String CONNECTION_TOPIC = "jms/mytopic";     

    public Producer(String message) {         
        ConnectionFactory connectionFactory = null;       
        Connection connection = null;         
        //Get the JNDI Context        
        try {        
            Context jndiContext = new InitialContext();      

            //Create the Connection Factory  and Connection     
            connectionFactory = (ConnectionFactory) jndiContext.lookup(Producer.CONNECTION_FACTORY);       
            connection = connectionFactory.createConnection();  

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

            publishToQueue(jndiContext, session, message);    
            session.commit();
        } catch (NamingException e) {            
        } catch (JMSException e) {    

        }finally{       
           //close connection     
        }   
    }        

    private void publishToQueue(Context jndiContext, Session session, String message) throws NamingException, JMSException{      
        //Create Queue     
        Queue queue = (Queue) jndiContext.lookup(Producer.CONNECTION_QUEUE); 

        //Create Message Producer       
        MessageProducer producer = session.createProducer(queue);     

        //Send TextMessage        
        producer.setDeliveryMode(DeliveryMode.PERSISTENT);
        TextMessage textMessage = session.createTextMessage();    
        textMessage.setText(message);
        producer.send(textMessage);  
        producer.close();

} 
 } 


public class Consumer {           
    private static final String CONNECTION_FACTORY = "connectionFactory";     
    private static final String CONNECTION_QUEUE = "jms/myqueue";    
    private static final String CONNECTION_TOPIC = "jms/mytopic";     

    private Message message;

    public Consumer() {        
        ConnectionFactory connectionFactory = null;       
        Connection connection = null;         
        //Get the JNDI Context        
        try {        
            Context jndiContext = new InitialContext();     

        //Create the Connection Factory       
        connectionFactory = (ConnectionFactory) jndiContext.lookup(Consumer.CONNECTION_FACTORY);       
        connection = connectionFactory.createConnection();  

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

        //Call methods to retrieve  message      
        message = getFromQueue(jndiContext, session);    
        session.commit();
        if(message!=null){
            System.out.println("Message Received");
        }
    } catch (NamingException e) {            
        // TODO Auto-generated catch block       
        e.printStackTrace();        
    } catch (JMSException e) {    
        // TODO Auto-generated catch block    
        e.printStackTrace();       

    }finally{       
        try {           
            if(connection != null){    
                connection.close();    
            }            
        } catch (JMSException e) {   
            e.printStackTrace();         
        }       
    }   
}        



private Message getFromQueue(Context jndiContext, Session session) throws NamingException, JMSException{      
    //Create new Queue
    Queue queue = (Queue)jndiContext.lookup(Consumer.CONNECTION_QUEUE);  
    //Create Message Consumer       
    MessageConsumer consumer = session.createConsumer(queue); 
    return consumer.receive(10000);    
}

public Message getMessage(){
    return message;
}
} 

最佳答案

抱歉,在另一个问题中找到了答案.. MessageConsumer is not consuming messages .. 我所需要的只是在 getFromQueue 方法之前调用 connection.start()

 public Consumer() {        
    ConnectionFactory connectionFactory = null;       
    Connection connection = null;         
    //Get the JNDI Context        
    try {        
        Context jndiContext = new InitialContext();     

    //Create the Connection Factory       
    connectionFactory = (ConnectionFactory) jndiContext.lookup(Consumer.CONNECTION_FACTORY);       
    connection = connectionFactory.createConnection();  

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

    //Call methods to retrieve  message   
     --> connection.start(); <--
    message = getFromQueue(jndiContext, session);    
    session.commit();
    if(message!=null){
        System.out.println("Message Received");
    }

关于java - 无法从jms队列接收消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17980936/

相关文章:

java - Appium:连接到 127.0.0.1:4723 [/127.0.0.1] 失败:连接被拒绝:连接

laravel - laravel 并行请求时如何使用队列

jms - 如何在 Websphere AS 中获取 MQTT 客户端 ID?

java - ActiveMQ Artemis 为 Spring Boot 客户端上定义的所有主题名称添加前缀 "jms.topic."

java - JMS 出站 channel 适配器基于 java 的配置

java - 如何从Java中的数据库应用程序的jar文件创建exe文件?

java 进程创建处于删除状态的 tmp/tmp* 文件,但增加了 Linux VM 中的磁盘利用率

java - Android 9 上相机 "1"的 Sinch VideoCaptureThread 问题

queue - 是否可以使用 cassandra 表作为基本队列

python - 一旦队列可用,便会逐步领取元素