最近又要使用MULE了。
其实我不是很喜欢这个东东,因为用的人少,资料比较少。
这次的应用主要是需要在受到POP3的邮件后,对取道邮件内容,根据邮件内容,连接数据库
查询资料,根据这个资料判断进入哪个ENDPOINT。
比较以前的MULE,增加了如下两个内容:
1、连接ORACLE数据库
2、在执行ENDPOINT的时候增加了一个判断
3、顺道把ACTIVEMQ的配置修改到了SPRING中
注意:
比较郁闷的是,一些比如ORACLE的驱动,IBATIS的JAR包都要拷贝到MULE的LIB的USER里面去,不然会找不到类
主要针对MULE的配置文件,email-config.xml做了修改:
<container-context
className="org.mule.extras.spring.SpringContainerContext"
name="spring">
<properties>
<property name="configFile"
value="classpath:/applicationContext.xml" />
</properties>
</container-context>
配置了,启动MULE的时候,启动SPRING的CONTAINER,
<connector name="activeMQConnector"
className="org.mule.providers.jms.JmsConnector">
<properties>
<property name="specification" value="1.1" />
<container-property name="connectionFactory"
reference="activeMqConnectionFactory" container="spring" />
</properties>
</connector>
ACTIVEMQ的配置转移到了SPRING的配置文件:applicationContext.xml中。
<model name="emailSample">
<mule-descriptor name="EmailActiveMQUM"
implementation="com.megaeyes.service.impl.EmailActiveMQManagerImpl">
<inbound-router>
<endpoint name="pop3email"
address="pop3://gateway:
[email protected]"
transformers="EmailMessageTransformer">
<properties>
<property name="checkFrequency" value="10000" />
<property name="deleteReadMessages"
value="true" />
</properties>
</endpoint>
</inbound-router>
<outbound-router>
<router
className="com.megaeyes.utils.TransactionJoiningRouter">
<endpoint name="fileEndpoint" address="file:///d:/logs">
<properties>
<property name="filename" value="emailActiveMQ.txt" />
<property name="outputAppend" value="true" />
</properties>
</endpoint>
<endpoint name="jmsEndpointOne" address="jms://MessageOne"
connector="activeMQConnector" />
<endpoint name="jmsEndpointTwo" address="jms://MessageTwo"
connector="activeMQConnector" />
</router>
</outbound-router>
</mule-descriptor>
</model>
ENDPOINT里面的OUTBOUND-ROUTER做了改变,改为用自己类来处理:
com.megaeyes.utils.TransactionJoiningRouter
这里加入了根据内容来判断选择不同的ENDPOINT的机制,选择把结果存放到不同的队列MESSAGEONE或者MESSAGETWO中
在自己写的类
com.megaeyes.utils.TransactionJoiningRouter.java中:
UMOEndpoint endpoint = getEndpoint(i, message);
MessageBean o = (MessageBean) message.getPayload();
// System.out.println("message class name: "
// + o.getClass().getName());
// System.out.println("endpoint name: " + endpoint.getName());
// TODO use the name of the endpoint to check if proess
if (endpoint.getName().equalsIgnoreCase("fileEndpoint")) {
continue;
}
endpoint.setRemoteSync(false);
try {
dispatch(session, message, endpoint);
} catch (UMOException e) {
throw new CouldNotRouteOutboundMessageException(message,
endpoint);
}
可以得到MESSAGE中传递过来的信息,根据信息判断是否需要dispatch当前的ENDPOINT,当然ENDPOINT的名字在这里也可以得到
贴出完整的email-config.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mule-configuration PUBLIC "-//MuleSource //DTD mule-configuration XML V1.0//EN"
"http://mule.mulesource.org/dtds/mule-configuration.dtd">
<mule-configuration id="Mule_Echo_Sample" version="1.0">
<description>This is the email mule ESB system</description>
<container-context
className="org.mule.extras.spring.SpringContainerContext"
name="spring">
<properties>
<property name="configFile"
value="classpath:/applicationContext.xml" />
</properties>
</container-context>
<connector name="activeMQConnector"
className="org.mule.providers.jms.JmsConnector">
<properties>
<property name="specification" value="1.1" />
<container-property name="connectionFactory"
reference="activeMqConnectionFactory" container="spring" />
</properties>
</connector>
<!--
<connector name="jdbcConnector"
className="org.mule.providers.jdbc.JdbcConnector">
<properties>
<container-property name="dataSource" reference="dataSource"
container="spring" />
</properties>
</connector>
-->
<transformers>
<transformer name="JMSMessageToString"
className="com.megaeyes.utils.JMSMessageTransformer"
returnClass="java.lang.String" />
<transformer name="HttpRequestToSoapRequest"
className="org.mule.providers.soap.transformers.HttpRequestToSoapRequest" />
<transformer name="EmailMessageTransformer"
className="com.megaeyes.utils.EmailMessageTransformer">
</transformer>
</transformers>
<model name="emailSample">
<mule-descriptor name="EmailActiveMQUM"
implementation="com.megaeyes.service.impl.EmailActiveMQManagerImpl">
<inbound-router>
<endpoint name="pop3email"
address="pop3://gateway:
[email protected]"
transformers="EmailMessageTransformer">
<properties>
<property name="checkFrequency" value="10000" />
<property name="deleteReadMessages"
value="true" />
</properties>
</endpoint>
</inbound-router>
<outbound-router>
<router
className="com.megaeyes.utils.TransactionJoiningRouter">
<endpoint name="fileEndpoint" address="file:///d:/logs">
<properties>
<property name="filename" value="emailActiveMQ.txt" />
<property name="outputAppend" value="true" />
</properties>
</endpoint>
<endpoint name="jmsEndpointOne" address="jms://MessageOne"
connector="activeMQConnector" />
<endpoint name="jmsEndpointTwo" address="jms://MessageTwo"
connector="activeMQConnector" />
</router>
</outbound-router>
</mule-descriptor>
</model>
</mule-configuration>
applicationContext.xml配置文件其实和spring的配置文件很相似:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="byName">
<bean id="activeMqConnectionFactory"
class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<value>tcp://localhost:61616</value>
</property>
</bean>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@192.168.10.11:1688:test</value>
</property>
<property name="username">
<value>dev</value>
</property>
<property name="password">
<value>dev</value>
</property>
</bean>
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:/sqlmap-config.xml</value>
</property>
<property name="useTransactionAwareDataSource" value="true"></property>
</bean>
<bean id="emailJmsDao"
class="com.megaeyes.dao.impl.EmailJmsDaoImpl" />
</beans>
自己的判断类TransactionJoiningRouter.java:
package com.megaeyes.utils;
import org.mule.impl.MuleMessage;
import org.mule.routing.outbound.FilteringOutboundRouter;
import org.mule.umo.UMOException;
import org.mule.umo.UMOMessage;
import org.mule.umo.UMOSession;
import org.mule.umo.endpoint.UMOEndpoint;
import org.mule.umo.routing.CouldNotRouteOutboundMessageException;
import org.mule.umo.routing.RoutingException;
import com.megaeyes.sms.gateway.databean.MessageBean;
public class TransactionJoiningRouter extends FilteringOutboundRouter {
public UMOMessage route(UMOMessage message, UMOSession session,
boolean synchronous) throws RoutingException {
final int endpointsCount = endpoints.size();
if (endpoints == null || endpointsCount == 0) {
}
if (enableCorrelation != ENABLE_CORRELATION_NEVER) {
boolean correlationSet = message.getCorrelationId() != null;
if (correlationSet
&& (enableCorrelation == ENABLE_CORRELATION_IF_NOT_SET)) {
logger
.debug("CorrelationId is already set, not setting Correlation group size");
} else {
// the correlationId will be set by the AbstractOutboundRouter
message.setCorrelationGroupSize(endpointsCount);
}
}
MessageBean o = (MessageBean) message.getPayload();
String mobileNumberStr = o.getTargetNumber();
if (StringUtil.isNotBlank(mobileNumberStr)) {
// 透传,分析号码
String[] mobileNumbers = mobileNumberStr.split(",");
if (mobileNumbers != null && mobileNumbers.length > 0) {
for (int j = 0; j < mobileNumbers.length; j++) {
String temp = mobileNumbers[j];
System.out.println(j + " temp number :" + temp);
if (checkIfMobilePhoneNumber(temp)) {
//update the targetNumber
MessageBean tMessage = (MessageBean)o.clone();
tMessage.setTargetNumber(temp);
UMOMessage tObj = new MuleMessage(tMessage);
//手机号码 放队列1
processMethod(tObj, session, endpointsCount, "jmsEndpointOne");
}else{
MessageBean tMessage = (MessageBean)o.clone();
tMessage.setTargetNumber(temp);
UMOMessage tObj = new MuleMessage(tMessage);
//小灵通号码 放队列2
processMethod(tObj, session, endpointsCount, "jmsEndpointTwo");
}
}
}
} else {
// 不透传 放队列1
processMethod(message, session, endpointsCount, "jmsEndpointOne");
}
return null;
}
private void processMethod(UMOMessage message, UMOSession session,
int endpointsCount, String methodName)
throws CouldNotRouteOutboundMessageException {
synchronized (endpoints) {
for (int i = 0; i < endpointsCount; i++) {
UMOEndpoint endpoint = getEndpoint(i, message);
if (!endpoint.getName().equalsIgnoreCase(methodName)) {
continue;
}
endpoint.setRemoteSync(false);
try {
dispatch(session, message, endpoint);
} catch (UMOException e) {
throw new CouldNotRouteOutboundMessageException(message,
endpoint);
}
}
}
}
// 小灵通 区号+电话号(7或8位)
// 手机 手机号码一定是13**********共11位数
private boolean checkIfMobilePhoneNumber(String mobileNumber) {
boolean flag = false;
if(StringUtil.isBlank(mobileNumber)){
return flag;
}
if(mobileNumber.startsWith("0")){
//小灵通
flag = false;
}else{
//手机
flag = true;
}
return flag;
}
}