码迷,mamicode.com
首页 > 其他好文 > 详细

基于ActiveMQ的RPC实现

时间:2015-02-28 14:49:24      阅读:696      评论:0      收藏:0      [点我收藏+]

标签:

        上一篇文《RPC的简单实现》中提到,可以利用MOM来实现RPC。这里就用实例来讲解如何使用Spring+ActiveMQ实现简易的rpc(这里简易的概念是指基本的远程调用,没有监控等功能)

        和传统的RPC框架(dubbo,hsf)一样,生产者需要有接口和实现,消费者需要有接口,接口最好以jar包的方式deploy到maven中。

  • 生产者        

        接口类代码

public interface IService{
	String getString();
}
          实现类代码
public class ServiceImpl implements IService {
	public String getString() {
		return "我是服务器。。。啦啦啦。。。";
	}
}

        jms的service.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<bean class="org.springframework.jms.listener.SimpleMessageListenerContainer">
		<property name="connectionFactory" ref="connectionFactory" />
		<property name="destination" ref="myQueue" />
		<property name="concurrentConsumers" value="3" />
		<property name="messageListener" ref="rpcService" />
		<property name="taskExecutor" ref="threadPool" />
	</bean>
	
	<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
		<property name="brokerURL" value="tcp://192.168.1.103:61616" />
	</bean>

	<bean id="myQueue" class="org.apache.activemq.command.ActiveMQQueue">
		<constructor-arg value="rpcQueue" />
	</bean>

	<bean id="rpcService" class="org.springframework.jms.remoting.JmsInvokerServiceExporter">
		<property name="serviceInterface" value="com.zaomeng.hijobs.IService" />
		<property name="service">
			<bean class="com.zaomeng.hijobs.ServiceImpl" />
		</property>
	</bean>
	
	<bean id="threadPool" class="org.springframework.core.task.SimpleAsyncTaskExecutor">
		<property name="daemon" value="true" />
		<property name="concurrencyLimit" value="300" />
		<property name="threadNamePrefix" value="SERVICE" />
	</bean>

</beans>
  • 消费者

        调用者代码

private void invoke() {
		IService service = (IService) ctx.getBean("rpcService"); 
		String result = service.getString();
}



        jms的client.xml代码

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
		<property name="brokerURL" value="tcp://192.168.1.103:61616" />
	</bean>

	<bean id="myQueue" class="org.apache.activemq.command.ActiveMQQueue">
		<constructor-arg value="rpcQueue" />
	</bean>

	<bean id="rpcService" class="org.springframework.jms.remoting.JmsInvokerProxyFactoryBean">
		<property name="serviceInterface" value="com.zaomeng.hijobs.IService" />
		<property name="connectionFactory" ref="connectionFactory" />
		<property name="queue" ref="myQueue" />
	</bean>

</beans>



基于ActiveMQ的RPC实现

标签:

原文地址:http://my.oschina.net/u/942651/blog/380725

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!