ILOG JRules User Guide > Executing Rules > Tasks > Executing a Ruleset Using Rule Execution Server > Invoking a Ruleset Using a Message-Driven Rule Bean > Invoking a Queue Request With a Reply

To use the JRules message-driven rule bean in a queue request with a reply, you must have the following:

A message client sends messages to the queue that the message-bean listens to.

To invoke a queue request with a reply:

  1. Enable the client to locate the connection factory and queue:
InitialContext ctx = new InitialContext();
 
   QueueConnectionFactory queueConnectionFactory = 
              (QueueConnectionFactory) 
              ctx.lookup("java:comp/env/jms/BRESQueueConnectionFactory");
   queueIn = (Queue) ctx.lookup( "java:comp/env/jms/BRESQueueIn");
   queueOut = (Queue) ctx.lookup("java:comp/env/jms/BRESQueueOut");
  1. Get the client to create the queue connection and session:
   queueConnection = queueConnectionFactory.createQueueConnection();
   queueSessionJMSTx = queueConnection.createQueueSession(true, 0);
  1. Get the client to send a message to the queue:
   userTransaction = (UserTransaction) 
                      ctx.lookup("java:comp/UserTransaction");
 
   ctx.close();
 
   QueueSender queueSender = queueSessionJMSTx.createSender(queueIn);
 
// Create the request message and initialization.
   ObjectMessage requestMessage = queueSessionJMSTx.createObjectMessage();
 
   String rulesetPath = "/ruleappinterceptor";
   String userData = "testRuleAppInterceptorWithJMSTx";
 
// Set the message header properties.
   jmsmessage.setStringProperty("ilog_rules_bres_mdb_rulesetPath",
                                 rulesetPath);
   jmsmessage.setStringProperty("ilog_rules_bres_mdb_userData",
                                 userData);
   jmsmessage.setStringProperty("ilog_rules_bres_mdb_status",
                                "request");
 
   requestMessage.setBooleanProperty(
                                "ilog_rules_bres_mdb_enableInterceptor",
                                true);
 
// Create the parameters.
   HashMap map = new HashMap();
   map.put("paramXml", getXmlString());
   SerializableObject javaObjectInput = new SerializableObject();
   javaObjectInput.value = "Value1";
   javaObjectInput.intValue = new Integer(3);
   map.put("paramJava", javaObjectInput);
 
// Set the parameters.
   requestMessage.setObject(map);
 
// Set the destination of the response message.
   requestMessage.setJMSReplyTo(queueOut);
 
// Send the message.
   userTransaction.begin();
 
   queueSender.send(requestMessage);
 
// Commit the JMSTx to send messages and begin the next JMSTx.
   userTransaction.commit();
 
   queueSender.close();
 
   StringBuffer selectorBuffer = new StringBuffer("JMSCorrelationID");
   selectorBuffer.append(" = '");
   selectorBuffer.append(requestMessage.getJMSMessageID());
   selectorBuffer.append("'");
 
   QueueReceiver queueReceiver = queueSessionJMSTx.createReceiver(queueOut,
                                selectorBuffer.toString(););
 
// Begin to receive messages.
   queueConnection.start();
 
// Waits 15000 ms.
   userTransaction.begin();
   Message messageOut = queueReceiver.receive(15000);
   userTransaction.commit();
 
// Stop receiving.
   queueReceiver.close();
 
   ObjectMessage responseMessage = (ObjectMessage) messageOut;
 
// Get the parameters.
   HashMap parameters = (HashMap) responseMessage.getObject();
 
   System.out.println(" canonical ruleset path executed " + 
              parameters.get("ilog_rules_bres_mdb_canonicalRulesetPath"));
   System.out.println("nb of fired rules " + 
              parameters.get("ilog.rules.firedRulesCount"));
   String xmlOut = (String) parameters.get("paramXml");
   SerializableObject javaObjectOutput = (SerializableObject) 
                                          parameters.get("paramJava");
  1. Close the session and connection:
queueSessionJMSTx.close();
queueConnection.close();

Related Concepts

Message-Driven Rule Beans

Related Tasks

Invoking a Queue Request With No Reply
Invoking a Topic Request With No Reply

Related Samples and Tutorials

How to Use a Message-Driven Rule Bean