首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

利用Camel调用远道对象

2012-08-26 
利用Camel调用远程对象利用Camel调用远程对象,很简答也很方便,只要定义好服务器和客户端即可。下面的例子可

利用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 {@Overridepublic String getName() {return "Tom";}@Overridepublic int getAge() {return 11;}@Overridepublic 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" value="tcp://localhost:61610" /></bean><bean id="studentExport" value="jms:queue:getStudent" /><property name="service" ref="student" /><property name="serviceInterface" value="com.zakisoft.camel.demo01.service.inft.StudentInterface" /></bean><bean id="student" /></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" 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;@BeforeClasspublic static void setUpServer() {serverContext = new ClassPathXmlApplicationContext("config01/camel-server.xml");}@AfterClasspublic static void tearDownServer() {if (serverContext != null) {serverContext.stop();}}@Testpublic 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. 所有源文件都在附件中。

热点排行