JMS messaging question

Discussion in 'Java' started by Blake Essing, Sep 15, 2006.

  1. Blake Essing

    Blake Essing Guest

    I'm having some trouble with JMS messages. I have the following method
    to put a message to a SonicMQ queue via JMS and I want to read the response
    message that comes back. I am putting the message successfully, but the
    code times out every time when trying to get the response. If anyone can see
    something I have wrong in the code, please let me know. I'm new to JMS so I
    assume that I'm missing something very basic. The getCorrelationId method
    just creates a correlation id based on some literals and a timestamp.



    public String sendXml(String pXML) throws ConnectionException {
    if (pXML == null)
    throw new ConnectionException("No XML data was entered.");

    String correlationId = null;

    try {
    QueueSession queueSession =
    mQueueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    QueueSender queueSender = queueSession.createSender(mQueue);

    TextMessage message = queueSession.createTextMessage();
    message.setJMSReplyTo((Queue)mJndiContext.lookup(mResponseQueue));
    message.setJMSExpiration(mTimeout);
    correlationId = getCorrelationId(1);
    message.setJMSCorrelationID(correlationId);
    message.setText(pXML);

    queueSender.send(message, DeliveryMode.PERSISTENT,
    Message.DEFAULT_PRIORITY, mTimeout);
    String JMSCorrelationId = message.getJMSMessageID();
    } catch (JMSException e) {
    throw new ConnectionException("Got JMS exception trying to
    connect to the queue " + mQueue + ".");
    } catch (NamingException ne) {
    throw new ConnectionException("Got naming exception trying to
    lookup queue " + mQueue + ".");
    }

    try {
    QueueSession queueSession =
    mQueueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue receiveQueue = (Queue)mJndiContext.lookup(mResponseQueue);
    QueueReceiver queueReceiver =
    queueSession.createReceiver(receiveQueue, correlationId);
    Message msg = queueReceiver.receive(mTimeout);

    if (msg != null) {
    String textMsg = null;
    if (msg instanceof TextMessage) {
    return ((TextMessage) msg).getText().toString();
    } else if (msg instanceof BytesMessage) {
    ByteArrayOutputStream bytes = new
    ByteArrayOutputStream();
    byte[] byteBuffer = new byte[1024];
    int read = 0;
    while ((read = ((BytesMessage)
    msg).readBytes(byteBuffer)) != -1) {
    bytes.write(byteBuffer, 0, read);
    }
    return new String(bytes.toByteArray());
    } else {
    throw new ConnectionException("Did not receive a
    readable message.");
    }
    } else {
    throw new ConnectionException("Did not receive a response in
    the time allotted.");
    }
    } catch (JMSException e) {
    throw new ConnectionException("Got JMS exception trying to
    connect to the queue " + mResponseQueue + ".");
    } catch (NamingException ne) {
    throw new ConnectionException("Got naming exception trying to
    lookup queue " + mResponseQueue + ".");
    }

    }
    Blake Essing, Sep 15, 2006
    #1
    1. Advertising

  2. Blake Essing

    Rob Guest

    "Blake Essing" <> wrote in message
    news:%wlOg.968394$084.466245@attbi_s22...
    >
    > I'm having some trouble with JMS messages. I have the following method
    > to put a message to a SonicMQ queue via JMS and I want to read the
    > response message that comes back. I am putting the message successfully,
    > but the code times out every time when trying to get the response. If
    > anyone can see something I have wrong in the code, please let me know.
    > I'm new to JMS so I assume that I'm missing something very basic. The
    > getCorrelationId method just creates a correlation id based on some
    > literals and a timestamp.


    It all depends on what the other side does in this situation, but how you
    would normally you would use the JMSCorrelationID is as follows :

    - The sender saves the JMSMessageID of the request message it sends
    - The receiver copies the JMSMessageID into the JMSCorrelationID of the
    reply
    - The sender matches the saved JMSMessageID of the request to the
    JMSCorrelationID of the reply

    Rob
    Rob, Sep 15, 2006
    #2
    1. Advertising

  3. >> I am putting the message successfully, but the
    code times out every time when trying to get the response.

    <i>
    What exactly do u mean by comes out? Is there any exception thrown ?
    Where do u see the flow stopping ?
    Lakshmi Ashok, Sep 15, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mayor Curley

    JMS: JBoss to Weblogic messaging

    Mayor Curley, Aug 25, 2003, in forum: Java
    Replies:
    0
    Views:
    413
    Mayor Curley
    Aug 25, 2003
  2. Thomas Stein
    Replies:
    0
    Views:
    760
    Thomas Stein
    Oct 18, 2004
  3. Sarah Wang
    Replies:
    2
    Views:
    4,321
    Steve Lamb
    May 29, 2004
  4. Replies:
    0
    Views:
    423
  5. Secret Squirrel

    question about messaging with web services

    Secret Squirrel, Apr 7, 2005, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    104
    Secret Squirrel
    Apr 7, 2005
Loading...

Share This Page