实习笔记4
2015-08-20 15:09
211 查看
spring webservice
Remote Method Invocation (RMI). 通过使用
和 透明远程通过RMI invokers (任何java接口).
Spring’s HTTP invoker. 提供特别策略 通过 HTTP 使java持久化, 支持任何java接口 (类似RMI invoker). 相应的类是
Hessian. 通过
HTTP为基础的协议 (provided by Caucho.)
Burlap. Caucho’s XML为基础的用来替代 Hessian. 相应类是
JAX-WS. 远程支持通过 JAX-WS (比如JAX-RPC, 在 Java EE 5 and Java 6被介绍).
JMS. JMS作为底层协议通过
AMQP. AMQP作为底层协议在spring AMQP project.
下面是spring例子:
Spring RMI
与EJB RMI区别:对安全上下文传播或远程事务传播的支持没有标准的支持,你可以自己插入插入安全框架或自定义安全凭据通过spring提供的接口
通过
服务将被绑定在
连接服务的客户端:
第二、
客户端调用:
配置:
Spring Web MVC
WEB-INF/applicationContext.xml
'web.xml'
客户端调用
JAX-WS
服务端
客户端
Refer to the Spring AMQP Reference Document 'Spring Remoting with AMQP' section for
more information.
http://my.oschina.net/xpbug/blog/264475
Remote Method Invocation (RMI). 通过使用
RmiProxyFactoryBean和
RmiServiceExporter接口,spring支持两种传统的RMI(
java.rmi.Remote和
java.rmi.RemoteException)
和 透明远程通过RMI invokers (任何java接口).
Spring’s HTTP invoker. 提供特别策略 通过 HTTP 使java持久化, 支持任何java接口 (类似RMI invoker). 相应的类是
HttpInvokerProxyFactoryBean和
HttpInvokerServiceExporter.
Hessian. 通过
HessianProxyFactoryBean和
HessianServiceExporter使用轻量级二进制
HTTP为基础的协议 (provided by Caucho.)
Burlap. Caucho’s XML为基础的用来替代 Hessian. 相应类是
BurlapProxyFactoryBean和
BurlapServiceExporter.
JAX-WS. 远程支持通过 JAX-WS (比如JAX-RPC, 在 Java EE 5 and Java 6被介绍).
JMS. JMS作为底层协议通过
JmsInvokerServiceExporter和
JmsInvokerProxyFactoryBean.
AMQP. AMQP作为底层协议在spring AMQP project.
下面是spring例子:
public class Account implements Serializable{ private String name; public String getName(){ return name; } public void setName(String name) { this.name = name; } }
public interface AccountService { public void insertAccount(Account account); public List<Account> getAccounts(String name); }
// the implementation doing nothing at the moment public class AccountServiceImpl implements AccountService { public void insertAccount(Account acc) { // do something... } public List<Account> getAccounts(String name) { // do something... } }
<bean id="accountService" class="example.AccountServiceImpl"> <!-- any additional properties, maybe a DAO? --> </bean>
Spring RMI
与EJB RMI区别:对安全上下文传播或远程事务传播的支持没有标准的支持,你可以自己插入插入安全框架或自定义安全凭据通过spring提供的接口
通过
RmiServiceExporter获取RMI
<bean class="org.springframework.remoting.rmi.RmiServiceExporter"> <!-- does not necessarily have to be the same name as the bean to be exported --> <property name="serviceName" value="AccountService"/> <property name="service" ref="accountService"/> <property name="serviceInterface" value="example.AccountService"/> <!-- defaults to 1099 --> <property name="registryPort" value="1199"/> </bean>
服务将被绑定在
'rmi://HOST:1199/AccountService'.
servicePort被省略(默认 0). 将会有匿名端口被用来通信. |
public class SimpleObject { private AccountService accountService; public void setAccountService(AccountService accountService) { this.accountService = accountService; } // additional methods using the accountService }
<bean class="example.SimpleObject"> <property name="accountService" ref="accountService"/> </bean> <bean id="accountService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"> <property name="serviceUrl" value="rmi://HOST:1199/AccountService"/> <property name="serviceInterface" value="example.AccountService"/> </bean>
Hessian or Burlap 调用服务通过 HTTP
'web.xml'
<servlet> <servlet-name>remoting</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>remoting</servlet-name> <url-pattern>/remoting/*</url-pattern> </servlet-mapping>
第一、'WEB-INF'下创建
'remoting-servlet.xml'
<bean id="accountService" class="example.AccountServiceImpl"> <!-- any additional properties, maybe a DAO? --> </bean>
<bean name="/AccountService" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="accountService"/>
<property name="serviceInterface" value="example.AccountService"/>
</bean>
http://HOST:8080/remoting/AccountService'.可以访问到结果
第二、
'WEB-INF/applicationContext.xml'
<servlet> <servlet-name>accountExporter</servlet-name> <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>accountExporter</servlet-name> <url-pattern>/remoting/AccountService</url-pattern> </servlet-mapping>
客户端调用:
<bean class="example.SimpleObject"> <property name="accountService" ref="accountService"/> </bean> <bean id="accountService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean"> <property name="serviceUrl" value="http://remotehost:8080/remoting/AccountService"/> <property name="serviceInterface" value="example.AccountService"/> </bean>
Burlap
配置:Hessian换成
Burlap
web.xml里配置安全,
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"> <property name="interceptors" ref="authorizationInterceptor"/> </bean> <bean id="authorizationInterceptor" class="org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor"> <property name="authorizedRoles" value="administrator,operator"/> </bean>
HTTP invokers
Spring Web MVC DispatcherServlet
<bean name="/AccountService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"> <property name="service" ref="accountService"/> <property name="serviceInterface" value="example.AccountService"/> </bean>
WEB-INF/applicationContext.xml
<bean name="accountExporter" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"> <property name="service" ref="accountService"/> <property name="serviceInterface" value="example.AccountService"/> </bean>
'web.xml'
<servlet> <servlet-name>accountExporter</servlet-name> <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>accountExporter</servlet-name> <url-pattern>/remoting/AccountService</url-pattern> </servlet-mapping>
客户端调用
<bean id="httpInvokerProxy" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"> <property name="serviceUrl" value="http://remotehost:8080/remoting/AccountService"/> <property name="serviceInterface" value="example.AccountService"/> </bean>
Web services
JAX-WS/** * JAX-WS compliant AccountService implementation that simply delegates * to the AccountService implementation in the root web application context. * * This wrapper class is necessary because JAX-WS requires working with dedicated * endpoint classes. If an existing service needs to be exported, a wrapper that * extends SpringBeanAutowiringSupport for simple Spring bean autowiring (through * the @Autowired annotation) is the simplest JAX-WS compliant way. * * This is the class registered with the server-side JAX-WS implementation. * In the case of a Java EE 5 server, this would simply be defined as a servlet * in web.xml, with the server detecting that this is a JAX-WS endpoint and reacting * accordingly. The servlet name usually needs to match the specified WS service name. * * The web service engine manages the lifecycle of instances of this class. * Spring bean references will just be wired in here. */ import org.springframework.web.context.support.SpringBeanAutowiringSupport; @WebService(serviceName="AccountService") public class AccountServiceEndpoint extends SpringBeanAutowiringSupport { @Autowired private AccountService biz; @WebMethod public void insertAccount(Account acc) { biz.insertAccount(acc); } @WebMethod public Account[] getAccounts(String name) { return biz.getAccounts(name); } }
JMS
package com.foo; public interface CheckingAccountService { public void cancelAccount(Long accountId); }
package com.foo; public class SimpleCheckingAccountService implements CheckingAccountService { public void cancelAccount(Long accountId) { System.out.println("Cancelling account [" + accountId + "]"); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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.xsd"> <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://ep-t43:61616"/> </bean> <bean id="queue" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg value="mmm"/> </bean> </beans>
服务端
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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.xsd"> <bean id="checkingAccountService" class="org.springframework.jms.remoting.JmsInvokerServiceExporter"> <property name="serviceInterface" value="com.foo.CheckingAccountService"/> <property name="service"> <bean class="com.foo.SimpleCheckingAccountService"/> </property> </bean> <bean class="org.springframework.jms.listener.SimpleMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory"/> <property name="destination" ref="queue"/> <property name="concurrentConsumers" value="3"/> <property name="messageListener" ref="checkingAccountService"/> </bean> </beans>
客户端
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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.xsd"> <bean id="checkingAccountService" class="org.springframework.jms.remoting.JmsInvokerProxyFactoryBean"> <property name="serviceInterface" value="com.foo.CheckingAccountService"/> <property name="connectionFactory" ref="connectionFactory"/> <property name="queue" ref="queue"/> </bean> </beans>
27.7 AMQP
Refer to the Spring AMQP Reference Document 'Spring Remoting with AMQP' section formore information.
用Spring JMS使异步消息变得简单
http://my.oschina.net/xpbug/blog/264475
相关文章推荐
- 火云开发课堂 - 《使用Cocos2d-x 开发3D游戏》系列 第五节:模型的加载与渲染
- 【转】在Tomcat配置JNDI数据源的三种方式
- JUnit常用注解
- GIT和SVN之间的五个基本区别
- IOS-企业开发者账号&邓白氏码申请记录
- 自定义导航栏的返回按钮
- 处理并发
- Dom元素的属性-卓有成效的使用
- DB2创建序列
- Android Call requires API level 11 (current min is 8)的解决方案
- Oracle基本查询 二
- jQuery+HTML5加入购物车代码分享
- Oracle\PLSQL Developer报“动态执行表不可访问,本会话的自动统计被禁止”的解决方案
- C#模拟PrtScn实现截屏
- [转载文章] 检测php网站是否已经被攻破的方法
- POJ-2676-Sudoku-DFS
- iOS系统网络抓包方法
- C#模拟PrtScn实现截屏
- SVN不错的入门
- Programming in Lua读书笔记