6.Spring Remoting (-)


Spring Remoting (-)

 

1. 使用Http 调用:首先在配置文件中定义所要暴露的接口,如下代码:

接收:

 <bean name="contactExporter"
		class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
		<property name="service" ref="contactService" />
		<property name="serviceInterface"
			value="com.apress.prospring3.ch16.service.ContactService" />
</bean>
 

定义ContactExporter作为暴露用的bean

属性service指定调用的具体实现类,属性serviceInterface指定调用的接口类型。

然后在web.xml添加暴露接口用代码,如下:

<!-- Spring Remoting with HTTP Invoker -->

	<servlet>
		<servlet-name>contactExporter</servlet-name>
		<servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>contactExporter</servlet-name>
		<url-pattern>/remoting/ContactService</url-pattern>
</servlet-mapping>
 

其中ContactExporter必须和上面定义的bean一致,然后定义url即可。

发送:

在spring配置文件里定义如下:

< bean id="remoteContactService"
		class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
		<property name="serviceUrl"
			value="http://localhost:8080/ch16-Remoting/remoting/ContactService" />
		<property name="serviceInterface"
			value="com.apress.prospring3.ch16.service.ContactService" />
</bean>
 

指定url和调用接口即可。

2. 使用JMS收发

接收:

在spring配置文件中添加如下配置

         <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"
		p:brokerURL="tcp://localhost:61616" />
	<bean id="simpleMessageListener"
		class="com.apress.prospring3.ch16.jms.listener.SimpleMessageListener" />
	<jms:listener-container container-type="default" 
		connection-factory="connectionFactory" acknowledge="auto">
		<jms:listener destination="prospring3" ref="simpleMessageListener"
			method="onMessage" />
</jms:listener-container>
 

首先定义一个connectionFactory,指定监听端口。

然后创建一个listener对象,要求实现javax.jms.MessageListener接口,用来处理接收到的消息。

最后定义一个listener-container,destination指定接受的queue名字和接收用的listener对象和调用方法。

这样系统就能收到指定queue发送的消息了。

发送:

首先在spring配置文件中添加如下配置

           <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"
		p:brokerURL="tcp://localhost:61616" />
	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
		<constructor-arg name="connectionFactory" ref="connectionFactory" />
		<property name="defaultDestinationName" value="prospring3" />
</bean>
 

指定url和queue名

然后在需要发送JMS的类里注入jmsTemplate,调用jmsTemplate.send()方法

详细信息可参考http://docs.oracle.com/javaee/5/tutorial/doc/bncdq.html 和

http://docs.oracle.com/javaee/6/api/javax/jms/Message.html

猜你喜欢

转载自wsjjasper.iteye.com/blog/1578194