This example shows you how to inject a JMSContext into an MDB and use it to send a reply to a JMS Client
The example leverages the JBoss Arquillian framework to run an Wildfly 8 instance and deploy the MDB.
download The latest Wildfly 8 from here and install.
set the JBOSS_HOME property to point to AS7 install directory
To run the example simply type mvn test
from the example directory
@Inject
javax.jms.JMSContext context;
@Resource(mappedName = "java:/queue/replyQueue")
Queue replyQueue;
jndi.properties
file in the directory config
final Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
env.put(Context.PROVIDER_URL, "remote://localhost:4447");
env.put(Context.SECURITY_PRINCIPAL, "guest");
env.put(Context.SECURITY_CREDENTIALS, "password");
initialContext = new InitialContext(env);
Queue queue = (Queue) initialContext.lookup("/queue/testQueue");
ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
try
(
// Step 6.Create a JMS Connection inside the try-with-resource block so it will auto close
JMSContext context = cf.createContext("guest", "password")
)
context.createProducer().send(queue, "This is a text message");
context.start();
Queue replyQueue = (Queue)initialContext.lookup("jms/queues/replyQueue");
String text = context.createConsumer(replyQueue).receiveBody(String.class);
finally
{
if (initialContext != null)
{
initialContext.close();
}
}