0

我想以同步方式处理来自 Glassfish 3 中 JMS 队列的所有消息,因此我尝试在 Glassfish 窗口中的 JMS Physical Destination 中将属性 Maximum Active Consumers 从 -1 更改为 1。我认为设置这个我将只有一个消费者同时执行 OnMessage()。我遇到的问题是,当我更改该属性时,出现此错误:

[I500]: Caught JVM Exception: org.xml.sax.SAXParseException: Content is not allowed in prolog.

[I500]: Caught JVM Exception: com.sun.messaging.jms.JMSException: Content is not allowed in prolog.

sendMessage Error [C4038]: com.sun.messaging.jms.JMSException: Content is not allowed in prolog.

如果有人知道使方法 onmessage() 同步的另一种方法,将不胜感激。这是我的消费类:

@MessageDriven(mappedName = "QueueListener", activationConfig = {
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
public class MessageBean implements MessageListener {



@Override
public void onMessage(Message message) {
    long t1 = System.currentTimeMillis();
    write("MessageBean has received " + message);
    try{

        TextMessage result=(TextMessage)message;
        String text=result.getText();
        write("OTAMessageBean message ID has resolved to " + text);
        int messageID=Integer.valueOf(text);

        AirProcessing aP=new AirProcessing();
        aP.pickup(messageID);


    }
    catch(Exception e){
        raiseError("OTAMessageBean error " + e.getMessage());
    }
   long t2 = System.currentTimeMillis();
   write("MessageBean has finished in " + (t2-t1)); 

}



}
4

2 回答 2

2

我遇到了同样的问题,我找到的唯一解决方案是设置一个Schedule每十秒轮询一次队列中的消息:

@Stateless
public class MyReceiver {
   @Resource(mappedName = "jms/MyQueueFactory")
   private QueueConnectionFactory connectionFactory;
   @Resource(mappedName = "jms/MyQueue")
   private Queue myQueue;
   private QueueConnection qc;
   private QueueSession session;
   private MessageConsumer consumer;


   @PostConstruct
   void init() {
       try {
         qc = connectionFactory.createQueueConnection();
         session = qc.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
         consumer = session.createConsumer(myQueue);
         qc.start();
       } catch (JMSException e) {
         throw new RuntimeException(e);
       }
   }

   @PreDestroy
   void cleanup() throws JMSException {
     qc.close();
   }

   @Schedule(hour = "*", minute = "*", second = "*/10", persistent = false)
   public void onMessage() throws JMSException {
     Message message;
     while ((message = consumer.receiveNoWait()) != null) {
       ObjectMessage objMsg = (ObjectMessage) message;
       Serializable content;
       try {
         content = objMsg.getObject();

         //Do sth. with "content" here

         message.acknowledge();
       } catch (JMSException ex) {
         ex.printStackTrace();
       }
    }
  }
}
于 2012-09-21T12:57:20.717 回答
1

JMS 本质上是异步的,您没有特定的配置来告诉 io 同步运行。您可以通过在各处添加消息传递和消费确认来模拟它,但这并不是 JMS 真正的工作方式。尝试 RMI在此处输入链接描述或 HTTP(或其他诸如 SOAP 或 REST Web 服务之类的东西)

于 2012-09-21T10:43:17.300 回答