您的位置:首页 > 其它

实习笔记4

2015-08-20 15:09 211 查看
spring webservice

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 for
more information.


用Spring JMS使异步消息变得简单

http://my.oschina.net/xpbug/blog/264475

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: