Search code examples
javaejbjms

JMS Get message/response from EJB


This is my sender class:

private void sendJMSMessage(Object data) throws JMSException {
        Connection con = null;
        Session s = null;
        try {
            con = context.createConnection();
            s = con.createSession(false, Session.CLIENT_ACKNOWLEDGE);
            MessageProducer producer = s.createProducer(glassFishQueue);
            ObjectMessage msg = s.createObjectMessage();
            ArrayList list = new ArrayList();
            list.add("name");
            msg.setObject(p);
            producer.send(msg);
        }

And my Message-driven Bean:

public void onMessage(Message message) {
    try {
        ObjectMessage om = (ObjectMessage) message;
        ArrayList al = (ArrayList) om.getObject();
        System.out.println("Msg: " + al.get(0));

    } catch (JMSException jex) {
        System.out.println("Exception: " + jex);
    }

I got the message sent from sender class but I need a message back from EJB to the sender. Im doing a web client with a table but I need to fill it getting the info from a database remotely, I really doesnt know what i should to use to do this, so if im doing right let me know or tell me any suggestion

Thank u


Solution

  • JMS is asynchronous, so it won't work in request-response style out of the box.

    If you want to send a reply, one way is to use a separate queue. Your MDB can write the response to this second queue and your client can listen to this queue by creating a QueueReceiver.

    Another way is to use QueueRequestor. From javadocs:

    It creates a TemporaryQueue for the responses and provides a request method that sends the request message and waits for its reply.

    Look here and here for QueueRequestor examples.