利用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. 所有源文件都在附件中。 :lol:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值