利用Camel调用远程对象
  b150W9sC7g95 2023年11月02日 61 0


利用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:


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
b150W9sC7g95