mule,就是传说中的一个ESB构架的实现,初步试用了一下,感觉它圈地圈得厉害,把
AXIS,XFIRE,POP3,SMTP,SOCKET,HTTP,JMS等通信连接方式都包括进来,可能是为了一些和老系统的接口
下载的地址是:
http://mule.codehaus.org/display/MULE/Home
下载后放置在路径:
D:\tool\mule-1.4.1
配置MULE_HOME
PATH里面把BIN加入进去
在路径D:\tool\mule-1.4.1\conf下有个配置文件
wrapper.conf
打开后修改
wrapper.java.classpath.2=%MULE_HOME%/lib/user/*.jar
wrapper.java.classpath.3=%MULE_HOME%/lib/boot/*.jar
wrapper.java.classpath.4=E:/project/megaeyes/Mail2Jms/WebRoot/WEB-INF/classes
这里配置的是MULE启动的时候要加载的JAR包和CLASSPATH
我的项目是MAIL2JMS,所以我直接指向了自己的CLASS目录,这样我的任何修改都直接生效了,
另外可以执行他的例子,ECHO.BAT
仿造他的例子,我自己写了一个从邮件服务器JAMES上收到信息后转发给ACTIVE-MQ的东东
类比较少,一一贴出来,实现类EmailManagerImpl.java:
package com.megaeyes.service.impl;
import org.mule.components.simple.LogComponent;
import org.mule.umo.UMOEventContext;
import com.megaeyes.service.EmailManager;
/**
*
* @author sillycat.luo
*
*/
public class EmailManagerImpl extends LogComponent implements EmailManager {
public Object onCall(UMOEventContext context) throws Exception {
// 调用基类打印出日志
// super.onCall(context);
// 调用transfer 得到对象
Object o = context.getTransformedMessage();
return o;
}
}
消息类,MessageBean.java:
/*
* Created on 2007-7-23 11:40:12
* @author hejing
*/
package com.megaeyes.sms.gateway.databean;
import java.io.Serializable;
public class MessageBean implements Serializable{
private static final long serialVersionUID = -1353347933934679190L;
private String topic; //标题
private String msgSrcID; //消息来源编号
private String msgTypeID; //消息类型编号
private String deviceName; //设备名称
private String deviceTypeName; //设备类型名称
private String deviceIP; //设备IP
private String msgTypeName; //消息类型名称
private String msgID; //消息流水号
private String severityName; //告警级别名称
private String faultTime; //故障产生时间
private String content; //内容
/**
* @return Returns the content.
*/
public String getContent() {
return content;
}
/**
* @param content The content to set.
*/
public void setContent(String content) {
this.content = content;
}
/**
* @return Returns the topic.
*/
public String getTopic() {
return topic;
}
/**
* @param topic The topic to set.
*/
public void setTopic(String topic) {
this.topic = topic;
}
/**
* @return Returns the deviceIP.
*/
public String getDeviceIP() {
return deviceIP;
}
/**
* @param deviceIP The deviceIP to set.
*/
public void setDeviceIP(String deviceIP) {
this.deviceIP = deviceIP;
}
/**
* @return Returns the deviceName.
*/
public String getDeviceName() {
return deviceName;
}
/**
* @param deviceName The deviceName to set.
*/
public void setDeviceName(String deviceName) {
this.deviceName = deviceName;
}
/**
* @return Returns the deviceTypeName.
*/
public String getDeviceTypeName() {
return deviceTypeName;
}
/**
* @param deviceTypeName The deviceTypeName to set.
*/
public void setDeviceTypeName(String deviceTypeName) {
this.deviceTypeName = deviceTypeName;
}
/**
* @return Returns the faultTime.
*/
public String getFaultTime() {
return faultTime;
}
/**
* @param faultTime The faultTime to set.
*/
public void setFaultTime(String faultTime) {
this.faultTime = faultTime;
}
/**
* @return Returns the msgID.
*/
public String getMsgID() {
return msgID;
}
/**
* @param msgID The msgID to set.
*/
public void setMsgID(String msgID) {
this.msgID = msgID;
}
/**
* @return Returns the msgSrcID.
*/
public String getMsgSrcID() {
return msgSrcID;
}
/**
* @param msgSrcID The msgSrcID to set.
*/
public void setMsgSrcID(String msgSrcID) {
this.msgSrcID = msgSrcID;
}
/**
* @return Returns the msgTypeID.
*/
public String getMsgTypeID() {
return msgTypeID;
}
/**
* @param msgTypeID The msgTypeID to set.
*/
public void setMsgTypeID(String msgTypeID) {
this.msgTypeID = msgTypeID;
}
/**
* @return Returns the msgTypeName.
*/
public String getMsgTypeName() {
return msgTypeName;
}
/**
* @param msgTypeName The msgTypeName to set.
*/
public void setMsgTypeName(String msgTypeName) {
this.msgTypeName = msgTypeName;
}
/**
* @return Returns the severityName.
*/
public String getSeverityName() {
return severityName;
}
/**
* @param severityName The severityName to set.
*/
public void setSeverityName(String severityName) {
this.severityName = severityName;
}
}
转换邮件信息的类EmailMessageTransformer.java:
package com.megaeyes.utils;
import java.io.IOException;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.mule.transformers.AbstractTransformer;
import org.mule.umo.transformer.TransformerException;
import com.megaeyes.sms.gateway.databean.MessageBean;
/**
* BEAN转换类
*
* @author sillycat.luo
*
*/
public class EmailMessageTransformer extends AbstractTransformer {
protected Log log = LogFactory.getLog(EmailMessageTransformer.class);
public EmailMessageTransformer() {
// 初始化要注册的对象类型
registerSourceType(MimeBodyPart.class);
registerSourceType(Message.class);
registerSourceType(String.class);
setReturnClass(String.class);
}
/**
* 实际调用转化类的方法
*/
protected Object doTransform(Object src, String encoding)
throws TransformerException {
MimeBodyPart mail = null;
if (src instanceof MimeBodyPart) {
mail = (MimeBodyPart) src;
} else {
log.error("email transfer should get MimeBodyPart src!");
}
String returnString = "";
MessageBean message = null;
try {
Object o = mail.getContent();
if (o instanceof String) {
returnString = (String) o;
} else if (o instanceof MimeMultipart) {
MimeMultipart mime = (MimeMultipart) o;
if (mime != null) {
BodyPart body = mime.getBodyPart(0);
if (body != null) {
returnString = (String) body.getContent();
}
}
} else {
log
.error("there is unknow part from the MimebodyPart.getContent");
}
} catch (IOException e) {
log.error(e);
} catch (MessagingException e) {
log.error("e");
}
// transform string 2 object
message = transFromString2Message(returnString);
return message;
}
/**
* 转化类的工具类
*
* @param src
* String like <root><name>sillycat.luo</name></root>
* @return MessageBean Object
*/
private MessageBean transFromString2Message(String src) {
MessageBean message = null;
if (StringUtil.isBlank(src)) {
log.error("src 2 transfer can't be null");
return message;
}
Document doc = XmlFileUtil.getDocumentSaxByXmlStr(src);
if (doc != null) {
message = new MessageBean();
message.setMsgSrcID(XmlFileUtil.getStringValue("MsgSrcID", doc));
message.setMsgTypeID(XmlFileUtil.getStringValue("FaultType", doc));
message
.setDeviceName(XmlFileUtil
.getStringValue("DeviceName", doc));
message.setDeviceTypeName(XmlFileUtil.getStringValue("DeviceType",
doc));
message.setDeviceIP(XmlFileUtil.getStringValue("DeviceIp", doc));
message.setMsgTypeName(XmlFileUtil.getStringValue("FaultTypeName",
doc));
message.setMsgID(XmlFileUtil.getStringValue("FaultID", doc));
message
.setSeverityName(XmlFileUtil
.getStringValue("Severity", doc));
message.setFaultTime(XmlFileUtil.getStringValue("FaultTime", doc));
} else {
log.error("xml doc string dom4j parse error!");
}
return message;
}
}
最主要的配置文件拉,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>
<connector name="activeMQConnector"
className="org.mule.providers.jms.JmsConnector">
<properties>
<property name="connectionFactoryJndiName"
value="ConnectionFactory" />
<property name="jndiInitialFactory"
value="com.megaeyes.utils.ActiveMQInitialContextFactory" />
<property name="specification" value="1.1" />
<property name="jndiDestinations" value="true" />
<property name="jndiProviderUrl"
value="tcp://localhost:61616" />
<map name="connectionFactoryProperties">
<property name="brokerURL"
value="tcp://localhost:61616" />
</map>
</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">
<properties>
<property name="checkFrequency" value="2000" />
<property name="deleteReadMessages" value="true" />
</properties>
</transformer>
</transformers>
<model name="emailSample">
<mule-descriptor name="MailUM"
implementation="com.megaeyes.service.impl.EmailManagerImpl">
<inbound-router>
<endpoint address="pop3://carl:[email protected]"
transformers="EmailMessageTransformer" />
</inbound-router>
<outbound-router>
<router
className="org.mule.routing.outbound.OutboundPassThroughRouter">
<endpoint address="jms://ConnectionFactory"
connector="activeMQConnector" />
</router>
</outbound-router>
</mule-descriptor>
<!--
<mule-descriptor name="ReSumeUM"
implementation="com.megaeyes.service.impl.EmailManagerImpl">
<inbound-router>
<endpoint address="jms://ConnectionFactory"
connector="activeMQConnector" transformers="JMSMessageToString"/>
</inbound-router>
<outbound-router>
<router
className="org.mule.routing.outbound.OutboundPassThroughRouter">
<endpoint address="stream://System.out" />
</router>
</outbound-router>
</mule-descriptor>
-->
</model>
</mule-configuration>
一个批处理执行文件,email.bat:
@echo off
call "%MULE_HOME%\bin\mule.bat" -config .\email-config.xml
这样就可以运行了,启动后会拦截所有发送到
[email protected]的邮件内容,然后拼装成为MessageBean.java放置在activeMQ的
queue里面。