简单的Mule3.4.0 CXF SOAP Java Application
?????????????? 以一个类似Echo的Java Application为例说明如何使用Mule来建立应用。后面会说明如何在Web应用程序中集成Mule。
1、在Eclipse中建立一个Java Application,为Application命令为MuleAppTest。
2、在Package Explorer视图中,选中刚刚建立的MuleAppTest项目,按下Alt+Enter键打开该项目的属性设置对话框,找到Java Build Path菜单,打开右边的Libraries标签页。
3、点击“Add External JARs...”按钮,将 %MULE_HOME%\lib\mule 目录和 %MULE_HOME%\lib\endorsed目录下的所有jar文件, %MULE_HOME%\lib\opt 目录下的所有jar文件,添加到项目的库路径中(%MULE_HOME%\bin\opt目录下的jar文件并不是必须的,您可以根据需要选择您要加入的包,本文实际上只使用了与Web Service相关的一些库文件)。
4、在源码目录(src)下建立。
/** * * <p>功能描述,该部分必须以中文句号结尾。<p> * * 创建日期 2013-8-16<br> * @author $Author$<br> * @version $Revision$ $Date$ * @since 3.0.0 */public interface LogService{ void log(String message);}
?
import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.mule.api.MuleEventContext;import org.mule.api.component.simple.LogService;import org.mule.api.lifecycle.Callable;import org.mule.util.StringMessageUtils;/** * <code>LogComponent</code> simply logs the content (or content length if it is a * large message) * * 创建日期 2013-8-16<br> * @author $Author$<br> * @version $Revision$ $Date$ * @since 3.0.0 */public class LogComponent implements Callable, LogService{ private static Log logger = LogFactory.getLog(LogComponent.class); public Object onCall(MuleEventContext context) throws Exception { String contents = context.getMessageAsString(); String msg = "Message received in service: " + context.getFlowConstruct().getName(); msg = StringMessageUtils.getBoilerPlate(msg + ". Content is: '" + StringMessageUtils.truncate(contents, 100, true) + "'"); log(msg); return context.getMessage(); } public void log(String message) { logger.info(message); }}
?
import org.mule.api.MuleEventContext;import org.mule.api.component.simple.EchoService;/** * * <code>EchoComponent</code> will log the message and return the payload back as * the result. * * 创建日期 2013-8-16<br> * @author $Author$<br> * @version $Revision$ $Date$ * @since 3.0.0 */public class EchoComponent extends LogComponent implements EchoService{ @Override public Object onCall(MuleEventContext context) throws Exception { super.onCall(context); return context.getMessage(); } public String echo(String echo) { return echo; }}
?
?
?
5、在源码目录(src)下建立一个mule-config.xml文件,为Mule配置入口端点,并将这个端点发布成一个基于axis的Web服务。
mule-config.xml
<?xml version="1.0" encoding="UTF-8"?><mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:cxf="http://www.mulesoft.org/schema/mule/cxf" xmlns:cxf-core="http://cxf.apache.org/core" xmlns:http="http://www.mulesoft.org/schema/mule/http" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/cxf http://www.mulesoft.org/schema/mule/cxf/current/mule-cxf.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"><spring:bean id="propertyConfigurer" value="websevice.properties" /> </spring:bean> <model name="BasicCxfTest"> <service name="echoService"> <inbound> <http:inbound-endpoint address="http://${host}:${port}/services/Echo" exchange-pattern="request-response" name="httpInbound"> <cxf:simple-service/> </http:inbound-endpoint> </inbound> <component /> </service> </model></mule>
?
?
测试:
import org.mule.api.MuleContext;import org.mule.api.MuleException;import org.mule.api.context.MuleContextFactory;import org.mule.config.spring.SpringXmlConfigurationBuilder;import org.mule.context.DefaultMuleContextFactory;/** * 检查是否发布成功的请求路径为: * http://localhost:8090/services/Echo?wsdl * <p>功能描述,该部分必须以中文句号结尾。<p> * * 创建日期 2013-8-16<br> * @author $Author$<br> * @version $Revision$ $Date$ * @since 3.0.0 */public class EchoMain { public static void main(String[] args) { try { String configFile = "mule-config.xml"; String[] configFileArr = new String[] {configFile }; MuleContextFactory muleContextFactory = new DefaultMuleContextFactory(); MuleContext context = muleContextFactory .createMuleContext(new SpringXmlConfigurationBuilder(configFileArr)); context.start(); } catch (MuleException t) { t.printStackTrace(); } }}
?
?
查看wsdl
http://localhost:8090/services/Echo?wsdl