To use the JRules message-driven rule bean in a queue request with a reply, you must have the following:
-
A JMS queue connection factory resource.
-
The JMS queue destination resource.
-
A queue destination to which the execution refers.
A message client sends messages to the queue that the message-bean listens to.
To invoke a queue request with a reply:
-
Enable the client to locate the connection factory and queue:
InitialContext ctx = new InitialContext();
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");
-
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");
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",
jmsmessage.setStringProperty("ilog_rules_bres_mdb_userData",
jmsmessage.setStringProperty("ilog_rules_bres_mdb_status",
requestMessage.setBooleanProperty(
"ilog_rules_bres_mdb_enableInterceptor",
// 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);
requestMessage.setObject(map);
// Set the destination of the response message.
requestMessage.setJMSReplyTo(queueOut);
queueSender.send(requestMessage);
// Commit the JMSTx to send messages and begin the next JMSTx.
userTransaction.commit();
StringBuffer selectorBuffer = new StringBuffer("JMSCorrelationID");
selectorBuffer.append(" = '");
selectorBuffer.append(requestMessage.getJMSMessageID());
selectorBuffer.append("'");
QueueReceiver queueReceiver = queueSessionJMSTx.createReceiver(queueOut,
selectorBuffer.toString(););
// Begin to receive messages.
Message messageOut = queueReceiver.receive(15000);
userTransaction.commit();
ObjectMessage responseMessage = (ObjectMessage) messageOut;
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");
-
Close the session and connection:
queueSessionJMSTx.close();
Related Concepts
Related Tasks
Related Samples and Tutorials