发送消息并从消息驱动的bean中获取响应

时间:2014-07-01 16:56:04

标签: java jms message-queue

我试图回复消息驱动的bean,但由于未知原因,我无法正确地进行通信。

这是有状态的bean。它发送一个对象消息,并在创建一个临时队列后接收响应。

@Stateful
public class BookingManagerBean implements BookingManagerBeanInterface
{

    @Inject
    @JMSConnectionFactory("jms/reservationProcessorQueueFactory")
    private JMSContext context;

    @Resource(mappedName = "jms/bookingProcessorQueueReceiver")
    private Queue bookingProcessorQueueReceiver;

    private Queue tempQueue;

    private JMSProducer messageProducer = null;
    private JMSConsumer messageConsumer = null;

    private String id_reservation = null;

    private Map<String,Object> map;

    @Override
    public String purchase()
    {                         

        try
        {
            sendMessageToBookingProcessorBean(object1, object2); // invia il messaggio contenente la prenotazione e il numero di punti utilizzati

            receiveMessageFromBookingProcessorBean(); // ricevi il messaggio dal MDB
        }
        catch(JMSException jmse)
        {
            System.err.println("An error occured " + jmse.toString());
            return (id_reservation);
        }

        return id_reservation;
    }

    private void sendMessageToBookingProcessorBean(Object object1, int object2) throws JMSException
    {                                  
        messageProducer = context.createProducer();

        messageProducer.send(bookingProcessorQueueReceiver,createMessageForBookingProcessorBean(object1, object2));
    }

    private Message createMessageForBookingProcessorBean(Object object1, int object2) throws JMSException
    {              
        ObjectMessage msg = context.createObjectMessage();

        tempQueue = context.createTemporaryQueue(); //create la queue temporanea dove sarà inviata la risposta
        messageConsumer = context.createConsumer(tempQueue);

        msg.setJMSReplyTo(tempQueue); 

        map = new HashMap<>();

        map.put("object1", object1);
        map.put("object2", object2);

        msg.setObject((Serializable) map);

        return msg;
    }

    private void receiveMessageFromBookingProcessorBean() throws JMSException
    {                
        Message msg = messageConsumer.receive();

        if(msg instanceof TextMessage)
        {
            TextMessage string = (TextMessage)msg;

            this.id_reservation = string.getText();
        }                
    }
}

这是消息驱动的bean

@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;

    @Inject
    @JMSConnectionFactory("jms/reservationProcessorQueueFactory")
    private JMSContext context;



    @Override
    public void onMessage(Message message)
    {                                  
        try
        {       
            ObjectMessage om = (ObjectMessage) message;

            map = (HashMap<String,Object>) om.getObject();


            System.out.println("Processing reservation...");


            sendIdReversationToBookingManagerBean(om.getJMSReplyTo(), "message response"); //prende la destinazione temporanea ed il messaggio
        }
        catch(UnsupportedEncodingException uee)
        {
            System.err.println("An error occured during the sending of the confirmation mail to " + reserv.getUsername().getEmail());
        }
        catch(MessagingException me)
        {
            System.err.println("An error occured during the sending of the email to " + reserv.getUsername().getEmail());
        }
        catch(JMSException e)
        {
            System.err.println("An error occured during the processing of the booking " + reserv.getId());
        }
    }


    private Message createMessageForBookingManagerBean(String message) throws JMSException
    {
        TextMessage msg = context.createTextMessage();

        msg.setText(message);

        return msg;
    }

    private void sendIdReversationToBookingManagerBean(Destination tempReceiverDestination, String message) throws JMSException
    {
        JMSProducer messageProducer = context.createProducer();

        messageProducer.send(tempReceiverDestination, createMessageForBookingManagerBean(id));
    }
}

我试过做一些调试,我注意到程序卡在接收方法中。我很确定有些东西不起作用。你可以帮助我吗?

0 个答案:

没有答案