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:
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.
session.commit();
). – Federico Sierra