2
votes

I have an JMS producer and one consumer the broker is ActiveMQ, refer code below:

Sender Code

package activemq;

import javax.jms.ConnectionFactory;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class PlatformNotifier {

public static void main(String[] args) throws Exception{

    ConnectionFactory connectionFactory=new ActiveMQConnectionFactory("tcp://localhost:61616");

    QueueConnection connection=(QueueConnection)connectionFactory.createConnection();
    connection.start();

    Session session=connection.createSession(true, Session.AUTO_ACKNOWLEDGE);

    Queue queue=session.createQueue("QUEUE.NOTIFICATION");

    MessageProducer producer=session.createProducer(queue);

    String message="from producer";
    TextMessage textMessage=session.createTextMessage(message);

    producer.send(textMessage);

    session.close();
    connection.stop();
    connection.close();

}//main closing

}//class closing

Receiver Code

package activemq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

public class ServiceNotifier {

public static void main(String[] args) throws Exception{

    System.out.println("Service Notifier");

    ConnectionFactory connectionFactory=new ActiveMQConnectionFactory("tcp://localhost:61616");

    Connection connection=connectionFactory.createConnection();
    connection.start();

    Session session=connection.createSession(true, Session.AUTO_ACKNOWLEDGE);

    Destination queue=session.createQueue("QUEUE.NOTIFICATION");

    MessageConsumer consumer=session.createConsumer(queue);

    Message message=consumer.receive(60*1000);

    System.out.println("Message = "+message);

    session.close();
    connection.stop();
    connection.close();

}//main closing

}//class closing

The problem is that ActiveMQ queue does not receive the message from the sender (refer screenshot):

enter image description here

When I send a message from the web console, it is received in the queue but from the producer nothing lands on the queue.

Another funny behaviour is that (as seen in the queue receiver code, the receiver exits after receiving the first message), again when I start the receiver, it receives the same message and keeps on doing it till I shut the server down and restart.

I am running on Windows 8.1, this is the first time I am encountering this problem. I would appreciate some help.

ActiveMQ -> 5.11.1

1

1 Answers

4
votes

Session session=connection.createSession(true, Session.AUTO_ACKNOWLEDGE); this creates a transacted session even if you have used AUTO_ACK mode. So you are creating transacted sessions but not handling the JMS transactions. Use session.commit() after you have sent/received a message.

Calling session.close() on open JMS transaction rolls back that JMS transaction. http://docs.oracle.com/javaee/7/api/javax/jms/Session.html#close()