标签:
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-core</artifactId> <version>2.17.0</version> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-spring</artifactId> <version>2.17.0</version> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-spring-redis</artifactId> <version>2.17.0</version> </dependency> </dependencies>
<?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"> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="localhost" /> <property name="port" value="9999" /> <property name="password" value="1234567890" /> </bean> <bean id="serializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" /> <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"> <route startupOrder="1"> <from uri="timer://foo?fixedRate=true&period=1000"/> <setHeader headerName="CamelRedis.Command"> <constant>SET</constant> </setHeader> <setHeader headerName="CamelRedis.Key"> <constant>keyOne</constant> </setHeader> <setHeader headerName="CamelRedis.Value"> <constant>valueOne</constant> </setHeader> <to uri="spring-redis://localhost:9999?connectionFactory=#connectionFactory&serializer=#serializer"/> </route> </camelContext> </beans>
<?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"> <bean id="serializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" /> <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"> <route startupOrder="1"> <from uri="timer://foo?fixedRate=true&period=1000"/> <setHeader headerName="CamelRedis.Command"> <constant>SET</constant> </setHeader> <setHeader headerName="CamelRedis.Key"> <constant>keyOne</constant> </setHeader> <setHeader headerName="CamelRedis.Value"> <constant>valueOne</constant> </setHeader> <to uri="spring-redis://localhost:9999?serializer=#serializer"/> </route> </camelContext> </beans>
/** * Created by sam on 5/10/16. */ public class App3 { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("context.xml"); context.start(); System.in.read(); } }
[sam@localhost redis-2.8.19]$ src/redis-cli -p 9999 -a 1234567890 127.0.0.1:9999> MONITOR OK 1462931627.929228 [0 127.0.0.1:50939] "PING" 1462931686.110952 [0 127.0.0.1:50943] "AUTH" "1234567890" 1462931686.120240 [0 127.0.0.1:50943] "SET" "keyOne" "valueOne" 1462931687.065705 [0 127.0.0.1:50943] "SET" "keyOne" "valueOne" 1462931688.066442 [0 127.0.0.1:50943] "SET" "keyOne" "valueOne" 1462931689.066169 [0 127.0.0.1:50943] "SET" "keyOne" "valueOne" 1462931690.065948 [0 127.0.0.1:50943] "SET" "keyOne" "valueOne" 1462931691.065674 [0 127.0.0.1:50943] "SET" "keyOne" "valueOne"
public class App4 { public static void main(String[] args) throws Exception { JedisConnectionFactory connectionFactory = new JedisConnectionFactory(); // 创建connectionFactory connectionFactory.setHostName("localhost"); connectionFactory.setPassword("1234567890"); connectionFactory.setPort(9999); SimpleRegistry registry = new SimpleRegistry(); connectionFactory.afterPropertiesSet(); // 必须要调用该方法来初始化connectionFactory registry.put("connectionFactory", connectionFactory); //注册connectionFactory registry.put("serializer", new StringRedisSerializer()); //注册serializer CamelContext context = new DefaultCamelContext(registry); context.addRoutes(new RouteBuilder() { public void configure() { errorHandler(deadLetterChannel("stream:out")); from("timer://foo?fixedRate=true&period=1000"). setHeader("CamelRedis.Command", constant("PUBLISH")). setHeader("CamelRedis.Channel", constant("testChannel")). setHeader("CamelRedis.Message", constant(new Date().toString())). to("spring-redis://localhost:9999?connectionFactory=#connectionFactory&serializer=#serializer"); } }); context.setTracing(true); context.start(); Thread.sleep(Integer.MAX_VALUE); context.stop(); } }
public class App4 { public static void main(String[] args) throws Exception { CamelContext context = new DefaultCamelContext(); context.addRoutes(new RouteBuilder() { public void configure() { errorHandler(deadLetterChannel("stream:out")); from("timer://foo?fixedRate=true&period=1000"). setHeader("CamelRedis.Command", constant("PUBLISH")). setHeader("CamelRedis.Channel", constant("testChannel")). setHeader("CamelRedis.Message", constant(new Date().toString())). to("spring-redis://localhost:9999"); } }); context.setTracing(true); context.start(); Thread.sleep(Integer.MAX_VALUE); context.stop(); } }
标签:
原文地址:http://www.cnblogs.com/zengbiaobiao2016/p/5481029.html