activemq与webservice整合
在多个系统中可能考虑到远程访问等的,采用WebService方式和远程系统的JMS消息系统交互。本文采用ActiveMQ和JAXWS实现。客户端通过webservice将消息发送给服务端的消息队列。
Java代码 package easyway.app.activemq.ws;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
/**
* 服务端接口
* @author longgangbai
*
*/
@WebService
public inte易做图ce HelloWorld {
/**
* 启动相关的服务方法
* @param name
* @return
*/
@WebMethod
String sayHi(@WebParam(name = "name") String name);
}
Java代码 package easyway.app.activemq.ws;
import javax.jws.WebService;
/**
* 服务端实现
* @author longgangbai
*
*/
@WebService
public class HelloWorldImpl implements HelloWorld {
/*
* (non-Javadoc)
* @see test.IHello#sayHi(java.lang.String)
*/
public String sayHi(String name) {
return "Hello " + name;
}
}
服务端通过WebService发布JMS消息的信息:
Java代码 package easyway.app.activemq.ws.server;
import javax.xml.ws.Endpoint;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.BrokerService;
import easyway.app.activemq.ws.HelloWorldImpl;
/**
* 通过WebService提供本地的JMS服务
* @author longgangbai
*
*/
public class ActiveMQWServer {
//设置JMS的URI的格式:jms:queue or topic:destination
private static final String JMS_ENDPOINT_URI = "jms:queue:jaxws.jmstransport.queue?timeToLive=1000"
+ "&jndiConnectionFactoryName=ConnectionFactory"
+ "&jndiInitialContextFactory"
+ "=org.apache.activemq.jndi.ActiveMQInitialContextFactory";
private ActiveMQWServer() {
}
public static void main(String args[]) throws Exception {
ActiveMQWServer server=new ActiveMQWServer();
//初始化Broker的方法
server.initBrokerService();
//启动JAXWS相关的 服务
server.launchJaxwsApi();
System.out.println("Server ready... Press any key to exit");
System.in.read();
System.out.println("Server exiting");
System.exit(0);
}
/**
* 启动ActiveMQ的Broker
* @throws Exception
*/
public void initBrokerService() throws Exception{
BrokerService brokerService=new BrokerService();
brokerService.addConnector( ActiveMQConnectionFactory.DEFAULT_BROKER_BIND_URL);
brokerService.setDataDirectory("target/activemq-data");
brokerService.start();
}
/**
* 发布WebService服务
*
*/
private void launchJaxwsApi() {
Endpoint.publish(JMS_ENDPOINT_URI, new HelloWorldImpl());
}
}
客户端通过webservice发送消息到服务端的消息队列:
Java代码 package easyway.app.activemq.ws.client;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import org.apache.cxf.transport.jms.spec.JMSSpecConstants;
import easyway.app.activemq.ws.HelloWorld;
/**
* 客户端通过WebService向服务端队列中发送消息
* @author longgangbai
*
*/
public final class ActiveMQWSClient {
//WebService请求的url的路径
private static final String JMS_ENDPOINT_URI = "jms:queue:jaxws.jmstransport.queue?timeToLive=1000"
+ "&jndiConnectionFactoryName=ConnectionFactory"
+ "&jndiInitialContextFactory"
+ "=org.apache.activemq.jndi.ActiveMQInitialContextFactory";
private static final QName SERVICE_QNAME =
new QName("http://ws.activemq.app.easyway/", "HelloWorldImplService");
private static final QName PORT_QNAME =
new QName("http://ws.activemq.app.easyway/", "HelloWorldPort");
private ActiveMQWSClient() {
}
/**
* 客户端调用WS服务
* @return
*/
private static HelloWorld createClientJaxWs() {
//创建相关的 服务
Service service = Service.create(SERVICE_QNAME);
// Add a port to the Service
service.addPort(PORT_QNAME, JMSSpecConstants.SOAP_JMS_SPECIFICATION_TRANSPORTID, JMS_ENDPOINT_URI);
return service.getPort(HelloWorld.class);
}
public static void main(String[] args) throws Exception {
HelloWorld client = createClientJaxWs();
补充:综合编程 , 其他综合 ,