`

利用Camel调用远程对象

阅读更多
利用Camel调用远程对象,很简答也很方便,只要定义好服务器和客户端即可。下面的例子可以直接运行,不过需要导入Camel和ActiveMQ的Jar包, 所有的Jar包都能在camel.apache.org下载。
代码如下:
1.先定义接口:
import java.util.Date;

public interface StudentInterface {
	
	public String getName();
	
	public int getAge();
	
	public Date getDate();
}

2.定义实现类:
public class StudentImpl implements StudentInterface {

	@Override
	public String getName() {
		return "Tom";
	}

	@Override
	public int getAge() {
		return 11;
	}

	@Override
	public Date getDate() {
		return new Date();
	}

}

3.定义服务器端配置文件:
<?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:context="http://www.springframework.org/schema/context"
	xmlns:camel="http://camel.apache.org/schema/spring" xmlns:broker="http://activemq.apache.org/schema/core"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
        http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.5.0.xsd">

	<camel:camelContext id="camel-server">
		<camel:route>
			<camel:from uri="jms:queue:getStudent" />
			<camel:to uri="studentExport" />
		</camel:route>
	</camel:camelContext>

	<broker:broker useJmx="false" persistent="false"
		brokerName="localhost">
		<broker:transportConnectors>
			<broker:transportConnector name="tcp"
				uri="tcp://localhost:61610" />
		</broker:transportConnectors>
	</broker:broker>

	<bean id="jms" class="org.apache.activemq.camel.component.ActiveMQComponent">
		<property name="brokerURL" value="tcp://localhost:61610" />
	</bean>

	<bean id="studentExport" class="org.apache.camel.spring.remoting.CamelServiceExporter">
		<property name="uri" value="jms:queue:getStudent" />
		<property name="service" ref="student" />
		<property name="serviceInterface" value="com.zakisoft.camel.demo01.service.inft.StudentInterface" />
	</bean>

	<bean id="student" class="com.zakisoft.camel.demo01.service.impl.StudentImpl" />

</beans>

4:定义客户端配置文件
<?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:camel="http://camel.apache.org/schema/spring"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

    <camel:camelContext id="camel-client">
    	<camel:template id="camelTemplate"/>
          <camel:proxy
            id="studentProxy"
            serviceInterface="com.zakisoft.camel.demo01.service.inft.StudentInterface"
            serviceUrl="jms:queue:getStudent"/>
    </camel:camelContext>
    
    <bean id="jms" class="org.apache.activemq.camel.component.ActiveMQComponent">
        <property name="brokerURL" value="tcp://localhost:61610"/>
    </bean>
    
</beans>

5.定义客户端和测试代码:
package com.zakisoft.camel.demo01.service;

import static org.junit.Assert.assertEquals;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zakisoft.camel.demo01.service.inft.StudentInterface;

public class CamelRemote4Test {

	protected static ConfigurableApplicationContext serverContext;

	@BeforeClass
	public static void setUpServer() {
		serverContext = new ClassPathXmlApplicationContext("config01/camel-server.xml");
	}

	@AfterClass
	public static void tearDownServer() {
		if (serverContext != null) {
			serverContext.stop();
		}
	}

	@Test
	public void testCamelRemotingInvocation() {
		ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(
				"config01/camel-client-remoting.xml");
		
		StudentInterface student = (StudentInterface) context.getBean("studentProxy");

		int age = student.getAge();

		assertEquals("Get a wrong response", 11, age);

		context.stop();
	}
}


6. 所有源文件都在附件中。
分享到:
评论

相关推荐

    CXF+RMI+HESSIAN

    这样,客户端就可以像调用本地方法一样调用远程服务了,而实际的通信则由RMI和Hessian处理。 总结,"CXF+RMI+HESSIAN"的整合为开发者提供了一种高效、简洁的分布式服务解决方案。CXF简化了Web服务的创建,RMI实现了...

    基于spring3.0的cxf发布webservice+client的

    这将创建一个代理类,可以像调用本地方法一样调用远程Web服务。 2. **配置客户端**:在Spring配置文件中,配置CXF客户端,包括URL、服务名称和端点地址。 3. **创建客户端实例**:在Java代码中,通过Spring的...

    EAI-realize-the-control-of-VRML-Java.zip_VRML_control

    此外,EAI框架如Apache Camel、MuleSoft等,提供了一套完整的工具集,便于构建数据流和业务流程,使得Java对VRML的控制更加灵活和高效。 为了实现更高级的功能,如用户交互和事件处理,Java可以利用VRML的事件模型...

    Akka Scala文档

    - **远程通信**:支持节点间的远程调用。 - **通信协议**:定义远程调用的协议。 - **网络优化**:提高远程调用效率。 ##### 5.10 序列化 - **序列化格式**:支持多种序列化方式。 - **序列化策略**:定义序列化的...

Global site tag (gtag.js) - Google Analytics