Java EE MDB Remote Failover Example

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.

Example step-by-step

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

  1. First we need to get an initial context so we can look-up the JMS connection factory and destination objects from JNDI. This initial context will get it's properties from the jndi.properties file in the directory config
  2.            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);
            
  3. We look up the JMS queue object from JNDI
  4.            Queue queue = (Queue) initialContext.lookup("/queues/inQueue");
            
  5. We look up the JMS connection factory object from JNDI
  6.            ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/jms/RemoteConnectionFactory");
            
  7. We create a JMS connection
  8.            connection = cf.createConnection("guest", "password");
            
  9. We create a JMS session. The session is created as non transacted and will auto acknowledge messages.
  10.            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            
  11. We create a JMS message producer on the session. This will be used to send the messages.
  12.           MessageProducer messageProducer = session.createProducer(queue);
           
  13. We create a JMS text messages that we are going to send.
  14.            TextMessage message = session.createTextMessage("This is a text message");
            
  15. We send the message to the queue
  16.            messageProducer.send(message);
            
  17. MDB receives the message
  18.            TextMessage textMessage = (TextMessage)message;
            
  19. Get and print the text
  20.            String text = textMessage.getText();
    
               System.out.println("message " + text);
            
  21. Create a JMS connection using the injected connection factory
  22.            conn = connectionFactory.createConnection();
            
  23. Create a JMS session
  24.            Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
            
  25. Create a producer
  26.            MessageProducer producer = sess.createProducer(replyQueue);
            
  27. Create a message and send it to the reply queue
  28.            producer.send(sess.createTextMessage("this is a reply"));
            
  29. Client looks up the reply queue
  30.            queue = (Queue) initialContext.lookup("/queues/outQueue");
            
  31. Create a consumer
  32.            MessageConsumer messageConsumer = session.createConsumer(queue);
            
  33. Start the connection
  34.            connection.start();
            
  35. Receive the message and print it out
  36.            message = (TextMessage) messageConsumer.receive(20000);
    
                   System.out.println("message.getText() = " + message.getText());
            
  37. Kill the live server. At this point both the standalone client and the MDB will fail-over to the backup.
  38.            killer.kill();
            
  39. Create another message
  40.            message = session.createTextMessage("This is another text message");
            
  41. Send the message
  42.            producer.send(message);
            
  43. The MDB will receive the message and send another message to the reply queue which the client then receives
  44.            message = (TextMessage) messageConsumer.receive(20000);
            
  45. And finally, always remember to close your JMS connections and resources after use, in a finally block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
  46.            finally
               {
                  if (initialContext != null)
                  {
                    initialContext.close();
                  }
                  if (connection != null)
                  {
                     connection.close();
                  }
               }