Writing a queue request with a reply

To call a ruleset from a message-driven rule bean, you can write a queue request with a reply.

About this task

To use message-driven rule beans in a queue request with a reply, you need a JMS queue connection factory resource, the JMS queue destination resource, and a queue destination to which the execution refers. A message client sends messages to the queue on which the message bean listens.

Procedure

  1. Enable the client to locate the connection factory and queue:
    InitialContext ctx = new InitialContext();
    
    	QueueConnectionFactory queueConnectionFactory = 
    			(QueueConnectionFactory)
    			ctx.lookup("jms/BRESQueueConnectionFactory");    
    	queueIn = (Queue) ctx.lookup( "jms/BRESQueueIn");    
    	queueOut = (Queue) ctx.lookup("jms/BRESQueueOut");
  2. Get the client to create the queue connection and session:
    queueConnection = queueConnectionFactory.createQueueConnection();
       queueSessionJMSTx = queueConnection.createQueueSession(true, 0);
  3. Get the client to send a message to the queue:
    userTransaction = (UserTransaction) 
                          ctx.lookup("java:comp/UserTransaction");
    
       ctx.close();
    
       QueueSender queueSender = queueSessionJMSTx.createSender(queueIn);
    1. Create the request message and initialization.
      ObjectMessage requestMessage = queueSessionJMSTx.createObjectMessage();
      
         String rulesetPath = "/ruleappinterceptor";
         String userData = "testRuleAppInterceptorWithJMSTx";
    2. Set the message header properties.
      requestMessage.setStringProperty("ilog_rules_bres_mdb_rulesetPath",
                                       rulesetPath);
         requestMessage.setStringProperty("ilog_rules_bres_mdb_userData",
                                       userData);
         requestMessage.setStringProperty("ilog_rules_bres_mdb_status",
                                      "request");
      
         requestMessage.setBooleanProperty(
                                      "ilog_rules_bres_mdb_enableInterceptor",
                                      true);
    3. Create and set 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);
      requestMessage.setObject(map);
    4. Set the destination of the response message.
      requestMessage.setJMSReplyTo(queueOut);
    5. Send the message.
      userTransaction.begin();
      
         queueSender.send(requestMessage);
    6. 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());
    7. Start message reception, then stop it after 15 seconds.
      queueConnection.start();
      
         userTransaction.begin();
         Message messageOut = queueReceiver.receive(15000);
         userTransaction.commit();
      
         queueReceiver.close();
      
         ObjectMessage responseMessage = (ObjectMessage) messageOut;
    8. Get the parameters, such as the canonical ruleset path and number of rules executed.
      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");
  4. Close the session and connection:
    queueSessionJMSTx.close();
    queueConnection.close();