基於ActiveMQ的RPC實現

        上一篇文《RPC的簡單實現》中提到,能夠利用MOM來實現RPC。這裏就用實例來說解如何使用Spring+ActiveMQ實現簡易的rpc(這裏簡易的概念是指基本的遠程調用,沒有監控等功能) java

        和傳統的RPC框架(dubbo,hsf)同樣,生產者須要有接口和實現,消費者須要有接口,接口最好以jar包的方式deploy到maven中。 spring

  • 生產者        

        接口類代碼 apache

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代碼 maven

<?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>
相關文章
相關標籤/搜索