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 the WildFly 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;
final Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
env.put(Context.PROVIDER_URL, "http-remoting://localhost:8080");
initialContext = new InitialContext(env);
Queue queue = (Queue)initialContext.lookup("jms/queues/testQueue");
ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("jms/RemoteConnectionFactory");
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();
}
}