訊息驅動批次規則引擎 (3) -時間觸發

時間觸發程式

package com.emprogria; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; import org.easyrules.core.AnnotatedRulesEngine; import org.quartz.Job; import org.quartz.JobDataMap; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.JobKey; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.Connection; import com.rabbitmq.client.Channel; import com.rabbitmq.client.QueueingConsumer; public class BizRuleSet implements Job { private final Logger myLog = LoggerFactory.getLogger(getClass()); private final long WAIT_FOR_MSG = 30 * 1000L; public BizRuleSet() { } public boolean CheckRules(String nameFriend, Properties actionResult) { boolean OK = false; Properties dbProps = new Properties(); dbProps.put("user", "job1"); dbProps.put("password", "job1"); if (!actionResult.isEmpty()) { if (actionResult.getProperty("FriendshipRule") == "1" && actionResult.getProperty("AgeRule") == "2") { java.sql.Connection dbConn; try { dbConn = DriverManager.getConnection( "jdbc:derby:BatchRules;create=true", dbProps); Statement sqlStatement = dbConn.createStatement(); String sqlString = String .format("SELECT isFriend, ageFriend FROM FriendList WHERE nameFriend='%s'", nameFriend); ResultSet sqlResultSet = sqlStatement .executeQuery(sqlString); if (sqlResultSet.next()) { OK = true; this.myLog .warn(String.format( "Name=%s, Friend=%d, Age=%d", nameFriend, sqlResultSet.getInt(1), sqlResultSet.getInt(2))); if (actionResult.getProperty("CreditRule") != null) { this.myLog.warn("Credit:" + actionResult.getProperty("CreditRule")); } } else { this.myLog.warn("SQL:" + sqlString); } } catch (SQLException e) { this.myLog.warn("SELECT:" + e.getMessage()); } } } return OK; } public void receiveMessages(String hostName, String queueName, JobKey jobKey) { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(hostName); Connection mqConnection; try { mqConnection = factory.newConnection(); Channel mqChannel = mqConnection.createChannel(); mqChannel.queueDeclare(queueName, true, false, false, null); QueueingConsumer mqConsumer = new QueueingConsumer(mqChannel); mqChannel.basicConsume(queueName, true, mqConsumer); QueueingConsumer.Delivery mqDelivery = null; while ((mqDelivery = mqConsumer.nextDelivery(WAIT_FOR_MSG)) != null) { String mqMessage = new String(mqDelivery.getBody()); String jobParams[] = mqMessage.split("\\|"); String nameFriend = jobParams[0]; String friendshipString = jobParams[1]; String ageString = jobParams[2]; Properties actionResult = new Properties(); AnnotatedRulesEngine rulesEngine = new AnnotatedRulesEngine(); FriendshipRule myFriendshipRule = new FriendshipRule( actionResult); AgeRule myAgeRule = new AgeRule(actionResult); myFriendshipRule.setInput(nameFriend.trim(), friendshipString.trim()); myAgeRule.setInput(nameFriend.trim(), ageString.trim()); rulesEngine.registerRule(myFriendshipRule); rulesEngine.registerRule(myAgeRule); rulesEngine.fireRules(); String anser = "X"; if (this.CheckRules(nameFriend.trim(), actionResult)) { anser = "O"; } this.myLog.warn(String.format("%s:[%s=%s]", jobKey.toString(), mqMessage, anser)); } mqChannel.close(); mqConnection.close(); } catch (Exception e) { this.myLog.error("MQ:" + e.getMessage()); } } @Override public void execute(JobExecutionContext jobContext) throws JobExecutionException { JobKey jobKey = jobContext.getJobDetail().getKey(); JobDataMap dataMap = jobContext.getJobDetail().getJobDataMap(); String jobParamList = dataMap.getString("jobParam"); String jobParams[] = jobParamList.split("\\|"); if (jobParams.length == 2) { String hostName = jobParams[0]; String queueName = jobParams[1]; this.myLog.warn(String.format("%s:[%s:%s]", jobKey.toString(), hostName, queueName)); this.receiveMessages(hostName, queueName, jobKey); } else { this.myLog.warn(String.format("%s:[%s]", jobKey.toString(), jobParamList)); } } }