1
votes

I set up an EJB project to send a persistence entity object to an MDB using JMS. I'm using JBoss EAP 7 using Apache ActiveMQ as the messaging provider. I set up the ConnectionFactory and Queue like so:

AccountAccessQueue

ConnectionFactory

This is my message producer that receives an 'Account' entity object as parameter and sends it to the queue:

EntityEnqueueBean.java

@Stateless
@LocalBean
public class EntityEnqueueBean {

  Context context = null;

  public void enqueueEntity(Account accountEntity) {    
      try {
          context = new InitialContext();
          ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("java:/ConnectionFactory");
          Destination queue = (Destination) context.lookup("java:/jms/queue/AccountAccessQueue");
          Connection connection = connectionFactory.createConnection();
          Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
          MessageProducer messageProducer = session.createProducer(queue);
          ObjectMessage objectMessage = session.createObjectMessage(accountEntity);
          messageProducer.send(objectMessage);

          messageProducer.close();
          session.close();
          connection.close();
      } catch (JMSException | NamingException e) {
          e.printStackTrace();
        }
   }
}

The MDB receives the message from the queue and processes it:

AccountPersistenceMDB.java

@MessageDriven(
    activationConfig = { @ActivationConfigProperty(
            propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
            @ActivationConfigProperty( propertyName = "destination", 
            propertyValue ="java:/jms/queue/AccountAccessQueue")
    })
public class AccountPersitenceMDB implements MessageListener {

  public void onMessage(Message message) {
      ObjectMessage objectMessage = null;
      objectMessage = (ObjectMessage) message;
      try {
          Account accountEntity = (Account) objectMessage.getObject();
          System.out.println("MDB accessCount: " + accountEntity.getAccessCount());
      } catch (JMSException e) {
          e.printStackTrace();
      }
  }
}

Not sure what I'm getting wrong. I probably got the code to set up the ConnectionFactory and the queue wrong but I'm not sure since I'm new to Java EE. Any help is appreciated.

1
Any stack trace on the server logs ? Any exceptions ?Ramachandran.A.G
@RamachandranGA no, everything just works, but the part where the MDB should be displaying results doesn't work. Also, monitoring the queue on JBoss admin panel shows that no messages were ever sent on the queue. I'm not able to pinpoint what I'm doing wrong.Nitin Paul
maybe there's a problem with the transaction, and your message is not being commited.. try changing the session to this.. Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);mendieta
As @mendieta say, setting the transacted parameter to true means you want to manually commit or roll back the session (eg: session.commit();).Federico Sierra

1 Answers

1
votes

Thanks to @mendieta I fixed the issue. I read up on JMS sessions and it made perfect sense. changing this line in EntityEnqueueBean.java did the trick:

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