How to configure MDB properties dynamically
A simple and effective way to configure the MDB properties dynamically with JBoss EAP and WildFly is via System Properties. All you have to do is coding your MDB Properties and then enable Property replacement:
import java.util.logging.Logger;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
@MessageDriven(name = "HelloWorldQueueMDB", activationConfig = {
@ActivationConfigProperty(propertyName = "destinationLookup", propertyValue = "${property.hello.queue}"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge")})
public class HelloQueueMDB implements MessageListener {
public void onMessage(Message rcvMessage) {
TextMessage msg = null;
try {
if (rcvMessage instanceof TextMessage) {
msg = (TextMessage) rcvMessage;
System.out.println("Received Message from queue: " + msg.getText());
} else {
System.out.println("Wrong type of message: " + rcvMessage.getClass().getName());
}
} catch (JMSException e) {
throw new RuntimeException(e);
}
}
}
Now before deploying the MDB, you need to configure the “ee” subsystem to enable MDB annotation property substitution and set the properties needed by the MDB. From the CLI:
batch /subsystem=ee:write-attribute(name=annotation-property-replacement,value=true) /system-property=property.hello.queue:add(value=java:/queue/HELLOWORLDMDBPropQueue) run-batch reload
Recommended Articles
Read Initialization Properties in EJB: A Simple Approach with Java and Singleton EJBs
Learn how to read properties from a file in an EJB-centric application using Java and Singleton EJBs. Get started with a simple example and understand the benefits of storing properties in a variable class.
Learn How to Code Simple Message-Driven Bean (MDB) and Client Interaction in Enterprise Java
Master coding a simple MDB and Client interaction for asynchronous messaging with this tutorial. #EnterpriseJava #MDB #CloudNative
Learn How to Use Hibernate Connection Properties in persistence.xml for Database Connectivity
Master Hibernate connection settings and configure your persistence.xml file for seamless database connectivity. #Hibernate #JavaPersistence #WildFly
Customizing Load Balancing for EJBs in WildFly and JBoss EAP 6
Learn how to customize load balancing for EJBs in WildFly and JBoss EAP 6, including implementing a custom DeploymentNodeSelector for Stateless and Stateful Session Beans.