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
Procedure
- 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"); - Get the client to create the queue connection and session:
queueConnection = queueConnectionFactory.createQueueConnection(); queueSessionJMSTx = queueConnection.createQueueSession(true, 0); - 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.
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); - 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); - 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()); - 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; - 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");
- Create the request message and initialization.
- Close the session and connection:
queueSessionJMSTx.close(); queueConnection.close();
Parent topic: Calling a queue request with a reply