发送消息已成功发送,但是当我尝试从代理获取消息时,代理中不会显示任何消息

时间:2014-07-11 05:23:54

标签: glassfish jms openmq

我是JMS + OPenMq + Glassfish的新手,请通过发送成功发送消息并接收消息来给我....

我创建了两个不同的servlet程序,我已经部署在galssfish服务器中....这里我成功发送消息,但消费者无法消费消息......

制作人:

          Properties p = new Properties(); 
                               p.put("java.naming.factory.initial","com.sun.enterprise.naming.SerialInitContextFactory");
        p.put("java.naming.factory.url.pkgs","com.sun.enterprise.naming");
        p.put("java.naming.provider.url", "iiop://localhost:3700");
        InitialContext jndiContext = new InitialContext(p);
        TopicConnectionFactory connectionFactory = (TopicConnectionFactory) jndiContext.lookup("jms/HQTapicConnectionFactory");
        Topic topic = (Topic) jndiContext.lookup("jms/HqDestTopic");
        System.out.println(topic.getTopicName());
                    TopicConnection connection =  (TopicConnection) connectionFactory.createTopicConnection();
                    System.out.println(connection.toString());
        TopicSession session = connection.createTopicSession(true, Session.AUTO_ACKNOWLEDGE);  //createSession(false, Session.AUTO_ACKNOWLEDGE);
        TopicPublisher publisher = session.createPublisher(topic);
        ObjectMessage message = session.createObjectMessage();
        ArrayList<Employee> employeeList= new ArrayList<Employee>();
        Employee employee  = null;
        for (int i = 0; i < 5; i++) {
              employee =  new Employee();
             employee.setEmpid((100+i));
             employee.setName("devid"+i);
             employeeList.add(employee);
        }
       System.out.println(employeeList.size());
        message.setObject(employeeList);
        publisher.send(message);

消费者:

public void onMessage(消息消息){

  ObjectMessage objectMessage= (ObjectMessage) message;

try{

  System.out.println("Received the following message: ");
  Object object = objectMessage.getObject(); 

  if(object  instanceof ArrayList){
      ArrayList arrayList = (ArrayList)object;
      for (int i = 0; i < arrayList.size(); i++) {
          Object object1 = arrayList.get(i); 
         if(object1  instanceof Employee){
          Employee employee = (Employee)object1;
          System.out.println(employee.getEmpid());
          System.out.println(employee.getName());
             System.out.println();
         }
    }
  }

}
catch (JMSException e)
{
  e.printStackTrace();
}

}

我无法接收消息,

请帮我正确配置glassfish服务器中的代理。

,...感谢您的重播

1 个答案:

答案 0 :(得分:0)

如果您的消费者在servlet中,它只会捕获在同一时刻发送的消息(非常不可能) - 您正在使用主题,默认情况下不会缓冲。< / p>

使用队列(而不是主题)或编写一个永久运行(从而收听/接收)的独立程序。通常情况下,主题监听器在servlet中没有多大意义。