消息驱动的bean没有响应来自有状态会话bean

时间:2014-06-21 21:48:48

标签: java java-ee jms

我正在努力使工作成为消息驱动的bean,但没有任何成功。也许你可以帮助我。

这是我的MDB的代码非常简单

@MessageDriven(mappedName = "jms/bookingProcessorQueueReceiver", 
               activationConfig = {@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
                                   @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
public class BookingProcessorBean implements MessageListener
{    
    @PersistenceContext
    private EntityManager em;

    public BookingProcessorBean()
    {  
    }

    @Override
    public void onMessage(Message message)
    {
        Reservation reserv = null;

        if(message instanceof Reservation)
        {       
            try
            {

                    System.out.println("Order processing...");

                    reserv = (Reservation) message; // riceve l'utente

                    UserCredential user = reserv.getUsername(); //estrae l'utente

                    user.getReservationsList().add(reserv);

                    storeInDb(reserv, user);

            }
            catch(Exception e)
            {
                System.err.println("An error occured during the processing of the order" + e.toString());
            } 
         }
    }

    private void storeInDb(Reservation r, UserCredential u)
    {        
        em.persist(r);
        em.merge(u);
    }
}

这是调用它的有状态会话bean

@Stateful
public class BookingBean implements BookingBeanInterface
{
    @Resource(mappedName = "jms/bookingProcessorQueueReceiver")
    private Queue bookingProcessorQueueReceiver;
    @Resource(mappedName = "jms/reservationProcessorQueueFactory")
    private ConnectionFactory reservationProcessorQueueFactory;

    private Connection connection = null;
    private Session session = null;

    private MessageProducer messageProducer;

    private boolean SUCCESS;

    @Override
    public boolean purchase(Route r, UserCredential u, int passengers, int  luggages, double price)
    {                         
        Reservation reserv = setReservation(r, u, passengers, luggages, (float) price);

        try
        {
            sendMessageToBookingProcessorBean(reserv); // invia il messaggio contenente la prenotazione

            SUCCESS = true;

        }
        catch(JMSException jmse)
        {
            System.err.println("An error occured " + jmse.toString());
            return SUCCESS = false;
        }
        finally
        {
            try
            {    
                if (session != null)
                {
                    session.close();         
                }

                if (connection != null)
                {
                    connection.close();
                }       
            }
            catch(JMSException jmse)
            {
                Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Cannot close session", jmse);
            }
        } 

        return SUCCESS;
    }

    private void sendMessageToBookingProcessorBean(Object messageData) throws JMSException
    {                      
        connection = reservationProcessorQueueFactory.createConnection();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        messageProducer = session.createProducer(bookingProcessorQueueReceiver);

        messageProducer.send(createMessageForBookingProcessorBean(session, messageData));
    }

    private Message createMessageForBookingProcessorBean(Session session, Object messageData) throws JMSException
    {

        connection.start();

        ObjectMessage msg = session.createObjectMessage((Serializable) messageData);


        return msg;
    }    
}

我不知道我是否忘了什么。请检查代码并告诉我一切是否正常......奇怪的是,当我发送消息时,我没有收到来自MDB的任何回复。我放入其中的任何代码都没有执行。我的意思是例如我没有执行简单的system.out.println。

1 个答案:

答案 0 :(得分:1)

在比较中

if (message instanceof Reservation)

您的邮件可能属于技术类型(例如TextMessage或在您的情况下是ObjectMessage)。

你需要像

这样的东西
if (message instanceof ObjectMessage) {
  Object content = message.getObject();
  if (content instanceof Reservation) {
    // Cast and proceed