利用webservice跟jms实现系统间的数据同步之一

利用webservice和jms实现系统间的数据同步之一

要实现的功能就是:jmsserver系统从webserviceserver系统中通过webservice获取数据,jmsserver系统并把获取到的数据保存到数据库,最后,jmsserver系统再通过jms把这些数据发送到jmsclient系统,jmsclient系统接收jms消息并在控制台打印,完毕。
所用到的技术:srping、cxf、jms、atomikos、hibernate,其中cxf实现webservice、restfulservice,atomikos用于实现数据库和jms的分布式事务,spring用于集成cxf、jms、atomikos、hibernate,完毕。小弟初步学习此技术,此文章肯定存在许多不足之处,还请各位能够指教!


首先说明webserviceserver系统,结构图如下:


利用webservice跟jms实现系统间的数据同步之一

 

其中UserService用于实现webservice,内容如下:


 

package com.test;

import java.util.List;

import javax.jws.WebResult;
import javax.jws.WebService;

@WebService
public interface UserService
{
    @WebResult List<User> getUserList();
    
    User getUserStr();
}


UserServiceImpl实现webservice,内容如下:


 

package com.test;

import java.util.ArrayList;
import java.util.List;

public class UserServiceImpl implements UserService
{

    public List<User> getUserList()
    {
        List<User> users = new ArrayList<User>();
        users.add(new User((long)1, "zhangsan1", 100));
        users.add(new User((long)2, "zhangsan2", 200));
        return users;
    }
    
    public User getUserStr()
    {
        return new User((long)3, "lisi1", 300);
    }
}


其中RestfulService用于实现restfulwebservice,内容如下:


 

package com.test.restful;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

public interface RestfulService
{
    @GET
    @Path("/{name}")
    @Consumes({"application/json"})
    public String getUserByName(@PathParam("name") String name);
}


RestfulServiceImpl实现RestfulService,内容如下:


 

package com.test.restful;

import com.test.User;

public class RestfulServiceImpl implements RestfulService
{

    public String getUserByName(String name)
    {
        return new User((long)4, name, 20).toString();
    }

}


web.xml配置如下:


 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:application-*.xml</param-value>
	</context-param>
	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>

    <!-- 用于实现webservice -->
	<servlet>
		<servlet-name>CXFService</servlet-name>
		<servlet-class>
			org.apache.cxf.transport.servlet.CXFServlet
		</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

    <!-- url-pattern说明我们通过cxf发布的webservice和restful都位于service的路径下 -->
	<servlet-mapping>
		<servlet-name>CXFService</servlet-name>
		<url-pattern>/service/*</url-pattern>
	</servlet-mapping>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>


spring配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xmlns:jaxrs="http://cxf.apache.org/jaxrs"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.0.xsd
	http://cxf.apache.org/jaxws 
	http://cxf.apache.org/schemas/jaxws.xsd
	http://cxf.apache.org/jaxrs 
	http://cxf.apache.org/schemas/jaxrs.xsd">

    <!-- 用于cxf实现ws和rs的标签配置 -->
	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
	
	<!-- 用于spring注解 -->
	<context:component-scan base-package="com.test"/>

    <!-- 发布ws,其中address是此ws的名称 -->
    <jaxws:endpoint id="user" implementor="com.test.UserServiceImpl" address="/user"/>

    <bean id="restful" class="com.test.restful.RestfulServiceImpl"/>
    
    <!-- 发布rs,其中address是此rs的名称 -->
	<jaxrs:server id="restfulService" address="/restful">
		<jaxrs:serviceBeans>
			<ref bean="restful"/>
		</jaxrs:serviceBeans>
		<jaxrs:extensionMappings>
		    <entry key="json" value="application/json" />
            <entry key="xml" value="application/xml" />
        </jaxrs:extensionMappings>
	</jaxrs:server>
</beans>