This example shows you how to send a message to an MDB that is configured to consume from a live/backup pair.
The example will send deploy a simple MDB to one Wildfly instance. Then it will send a message to the live server of the live/backup pair which will be consumed by the MDB after which the MDB will send a reply message which will be consumed by the example program. Then the live server will be stopped so that the backup takes over and the process will be repeated.
Unlike the "Java EE MDB Remote Failover Static Example," this example uses a "dynamic" configuration for finding all the nodes. In other words it uses UDP multicast for server discovery.
The example leverages the JBoss Arquillian framework to run a WildFly instance and deploy the MDB.
download WildFly 8.0.0.Final from here and install.
set the JBOSS_HOME property to point to the WildFly install directory
type mvn verify
from the example directory to run
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, "http-remoting://localhost:8080"); initialContext = new InitialContext(env);
Queue queue = (Queue) initialContext.lookup("/queues/inQueue");
ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/jms/RemoteConnectionFactory");
connection = cf.createConnection("guest", "password");
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer messageProducer = session.createProducer(queue);
TextMessage message = session.createTextMessage("This is a text message");
messageProducer.send(message);
TextMessage textMessage = (TextMessage)message;
String text = textMessage.getText();
System.out.println("message " + text);
conn = connectionFactory.createConnection();
Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = sess.createProducer(replyQueue);
producer.send(sess.createTextMessage("this is a reply"));
queue = (Queue) initialContext.lookup("/queues/outQueue");
MessageConsumer messageConsumer = session.createConsumer(queue);
connection.start();
message = (TextMessage) messageConsumer.receive(20000);
System.out.println("message.getText() = " + message.getText());
killer.kill();
message = session.createTextMessage("This is another text message");
producer.send(message);
message = (TextMessage) messageConsumer.receive(20000);
finally
block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
finally
{
if (initialContext != null)
{
initialContext.close();
}
if (connection != null)
{
connection.close();
}
}