Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 向消息驱动bean发送消息并从中获取响应_Java_Jms_Message Queue - Fatal编程技术网

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

Java 向消息驱动bean发送消息并从中获取响应,java,jms,message-queue,Java,Jms,Message Queue,我试图对消息驱动bean做出响应,但由于未知原因,我无法使通信正常工作 这是有状态的bean。它发送一个对象消息,并在创建一个临时队列后接收响应 @Stateful public class BookingManagerBean implements BookingManagerBeanInterface { @Inject @JMSConnectionFactory("jms/reservationProcessorQueueFactory") private JMS

我试图对消息驱动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();
        }                
    }
}
@Stateful
公共类BookingManagerBean实现BookingManagerBean接口
{
@注入
@JMSConnectionFactory(“jms/reservationProcessorQueueFactory”)
私有JMSContext上下文;
@资源(mappedName=“jms/bookingProcessorQueueReceiver”)
专用队列预订处理器QueueReceiver;
专用队列;
私有JMSProducer messageProducer=null;
私有JMSConsumer messageConsumer=null;
私有字符串id_reservation=null;
私人地图;
@凌驾
公共字符串购买()
{                         
尝试
{
sendMessageToBookingProcessorBean(object1,object2);//因应信息内容和使用价值
receiveMessageFromBookingProcessorBean();//ricevi il messaggio dal MDB
}
捕获(jmse)
{
System.err.println(“发生错误”+jmse.toString());
返回(id_预订);
}
返回id\u预约;
}
私有void sendMessageToBookingProcessorBean(对象object1,int object2)引发JMSException
{                                  
messageProducer=context.createProducer();
send(bookingProcessorQueueReceiver,createMessageForBookingProcessorBean(object1,object2));
}
私有消息createMessageForBookingProcessorBean(对象object1,int object2)引发JMSException
{              
ObjectMessage msg=context.createObjectMessage();
tempQueue=context.createTemporaryQueue();//创建临时队列
messageConsumer=context.createConsumer(tempQueue);
msg.setJMSReplyTo(tempQueue);
map=新的HashMap();
map.put(“object1”,object1);
map.put(“object2”,object2);
msg.setObject((可序列化)映射);
返回味精;
}
private void receiveMessageFromBookingProcessorBean()引发JMSExException
{                
Message msg=messageConsumer.receive();
if(消息实例of TextMessage)
{
text消息字符串=(text消息)消息;
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));
    }
}
@MessageDriven(mappedName=“jms/bookingProcessorQueueReceiver”,
activationConfig={@ActivationConfigProperty(propertyName=“acknowledgeMode”,propertyValue=“Auto acknowledge”),
@ActivationConfigProperty(propertyName=“destinationType”,propertyValue=“javax.jms.Queue”)
})
公共类BookingProcessorBean实现MessageListener
{   
@持久上下文
私人实体管理者;
@注入
@JMSConnectionFactory(“jms/reservationProcessorQueueFactory”)
私有JMSContext上下文;
@凌驾
消息(消息消息)上的公共无效
{                                  
尝试
{       
ObjectMessage om=(ObjectMessage)消息;
map=(HashMap)om.getObject();
System.out.println(“处理预订…”);
SendIDReversationBookingManagerBean(om.getJMSReplyTo(),“消息响应”);//临时消息
}
捕获(不支持的编码异常uee)
{
System.err.println(“向“+reserv.getUsername().getEmail()”发送确认邮件时出错);
}
捕获(消息例外我)
{
System.err.println(“向“+reserv.getUsername().getEmail()”发送电子邮件时出错);
}
捕获(JME)
{
System.err.println(“预订处理过程中发生错误”+reserv.getId());
}
}
私有消息createMessageForBookingManagerBean(字符串消息)引发JMSExException
{
TextMessage msg=context.createTextMessage();
msg.setText(消息);
返回味精;
}
private void SendIDReversationBookingManagerBean(目标TempReceiveDestination,字符串消息)引发JMSExException
{
JMSProducer messageProducer=context.createProducer();
messageProducer.send(tempReceiverDestination,createMessageForBookingManagerBean(id));
}
}

我试着做了一些调试,我注意到程序卡在了receive方法中。我很确定有些东西不起作用。您能帮助我吗?

我使用了setJMSReplyTo方法来确保我在临时队列中得到响应mdb中的代码没有被删除。。。