mule cxf 入门篇

来源:百度文库 编辑:神马文学网 时间:2024/04/29 14:02:00
1 创建服务接口
/**
*
*/
package com.cimstech.service;
import javax.jws.WebService;
/**
* @author wanghongyu
*
*/
@WebService
public interface MessageHandlerService {
public String messageHandler(String message);
}
2 实现服务接口
public class MessageHandlerImp implements MessageHandlerService {
public MessageHandlerImp() {
}
public String messageHandler(String message) {
System.out.println("haha");
return "success";
}
}
3 配置mule配置文件
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:cxf="http://www.mulesource.org/schema/mule/cxf/2.2"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.mulesource.org/schema/mule/core/2.2
http://www.mulesource.org/schema/mule/core/2.2/mule.xsd
http://www.mulesource.org/schema/mule/cxf/2.2
http://www.mulesource.org/schema/mule/cxf/2.2/mule-cxf.xsd">










4 启动mule服务器
/**
*
*/
package com.cimstech.server;
import org.mule.MuleServer;
import org.mule.api.MuleContext;
import org.mule.api.MuleException;
import org.mule.api.config.ConfigurationException;
import org.mule.api.lifecycle.InitialisationException;
import org.mule.config.spring.SpringXmlConfigurationBuilder;
import org.mule.context.DefaultMuleContextFactory;
/**
* @author wanghongyu
*
*/
public class StartMule {
public boolean start(){
MuleContext context;
try {
context = new DefaultMuleContextFactory()
.createMuleContext(new SpringXmlConfigurationBuilder(
"mule-config.xml"));
context.start();
} catch (InitialisationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (MuleException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
/**
* @param args
*/
public static void main(String[] args) {
new StartMule().start();
}
}
5 启动客户端进行测试
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.cimstech.service.MessageHandlerService;
/**
*
*/
/**
* @author wanghongyu
*
*/
public class CreateClient {
/**
* @param args
*/
public static void main(String[] args) {
try {
BufferedReader reader = new BufferedReader(new FileReader(
"F:\\eclipse_jee\\workspace\\61968WS\\WebContent\\WEB-INF\\examples\\create_CustomerAgreementConfig.xml"));
String xml = "";
String line = reader.readLine();
while (line != null) {
xml += line;
line = reader.readLine();
}
reader.close();
//http://Dev1:8081/61968WS/services/MessageHandler
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
factory.setServiceClass(MessageHandlerService.class);
factory
.setAddress("http://localhost:8080/61968WS/messageHandler");
Object x = factory.create();
System.out.println("class name : " + x.getClass());
MessageHandlerService client = (MessageHandlerService) factory.create();
String result = client.messageHandler(xml);
System.out.println(result);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
mule 2.x的代码启动方式:
context = new DefaultMuleContextFactory()
.createMuleContext(new SpringXmlConfigurationBuilder(
"mule-config.xml"));
mule 3.0的启动方式:
MuleServer server = new MuleServer("mule-config.xml");
server.start(true, true);