tags:

views:

30

answers:

3

(JMS)Does JMSClient which is also producer in some cases recieve its own messages which it sends

+1  A: 

definitely yes, if it has a listener on a destination that it also produces messages on.

Ahmad
A: 

As you can see in the example below it is possible for a client to be both a producer and a consumer. It really depends how you set it up. Typically a client is either a consumer or a producer if you are doing async messaging. If you are doing request/reply then it would do both, and you would either use a correlationID or a messageID to keep track of your requests and replies. The sample below is for asynchronous communications.

    myConnFactory = new com.sun.messaging.ConnectionFactory();
    Connection myConn = myConnFactory.createConnection();
    //Create a session within the connection.
    Session mySess = myConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    myQueue = new com.sun.messaging.Queue("world");

    //Create a message producer.
    MessageProducer myMsgProducer = mySess.createProducer(myQueue);

    //Create a message consumer. (Use if going to read from the queue)
    MessageConsumer myMsgConsumer = mySess.createConsumer(myQueue);

    //Start the Connection
    myConn.start();

    //Create and send a message to the queue.
    TextMessage myTextMsg = mySess.createTextMessage();
    myTextMsg.setText("Hello World");
    System.out.println("Sending Message: " + myTextMsg.getText());
    myMsgProducer.send(myTextMsg);

    // The rest of the code is for reading from a queue - optional
    //Receive a message from the queue. 
    Message msg = myMsgConsumer.receive();

    //Retreive the contents of the message.
    if (msg instanceof TextMessage) {
        TextMessage txtMsg = (TextMessage) msg;
        System.out.println("Read Message: " + txtMsg.getText());
    }
Romain Hippeau
Looks like typo error. It should be "The sample below is for synchronous communications".
Sujee
@Sujee - rearranged the code. You were partially correct. Actually it was for async communications - If I wanted synchronous I would have waited in the get for either a message of a certain ID or a certain correlation ID.
Romain Hippeau
+1  A: 

I got the answer.. There is a noLocal Flag which one can set to not receive messages from same connection

Abhishek