以下是我的camel路由,用于发送定期消息并将队列中的消息转发到端点。
活动路线:
from("activemq:Queue.External?cacheLevelName=CACHE_CONSUMER&transacted=true")
.routeId("EventRoute")
.autoStartup(false)
.filter(messageFilter)
.process(eventTransformer)
.setHeader(Exchange.HTTP_METHOD, constant(HttpMethods.POST))
.setHeader(Exchange.ACCEPT_CONTENT_TYPE, constant("application/xml"))
.setProperty("eventEndpoint", constant(eventEndpoint))
.to(eventUri)
.process(eventResponse);
定期消息路由:
from("timer:monitor?fixedRate=true&period=" + (periodicMessageInterval() * 1000))
.routeId("periodicMessageRoute")
.autoStartup(false)
.process(periodicMessageTransformer)
.setHeader(Exchange.HTTP_METHOD, constant(HttpMethods.POST))
.setHeader(Exchange.ACCEPT_CONTENT_TYPE, constant("application/xml"))
.doTry()
.to(periodicMessageUri)
.process(periodicMessageResponse)
.doCatch(Exception.class)
.log(LoggingLevel.DEBUG, "Error response received: ${body}");
定期消息检测的路由初始化
builder.from("activemq:topic:Topic.Heartbeat?concurrentConsumers=1&maxConcurrentConsumers=1")
.routeId("periodic")
.log(LoggingLevel.DEBUG, "Periodic Message Received: ${id}")
.process(this);
builder.from("timer:monitor?fixedRate=true&period=" + monitoringIntervalInMilliseconds)
.routeId("timer")
.log(LoggingLevel.DEBUG, "Checking periodic message reception")
.process(exchange -> exchange.getIn().setBody(check()))
.choice().when(builder.body(Boolean.class)).to("direct:stopActiveMqRoutes")
.otherwise().to("direct:startActiveMqRoutes").end();
ProcessorDefinition routeDefinitionStop = builder.from("direct:stopActiveMqRoutes");
for (final String routeId : routeIds) {
routeDefinitionStop = routeDefinitionStop
.to("controlbus:route?routeId=" + routeId + "&action=status")
.choice().when(builder.body().isNotEqualTo("Stopped"))
.log(LoggingLevel.INFO, "Stopping route execution: " + routeId)
.to("controlbus:route?routeId=" + routeId + "&action=stop&async=true")
.end();
}
routeDefinitionStop.end();
ProcessorDefinition routeDefinitionStart = builder.from("direct:startActiveMqRoutes");
for (final String routeId : routeIds) {
routeDefinitionStart = routeDefinitionStart
.to("controlbus:route?routeId=" + routeId + "&action=status")
.choice().when(builder.body().isNotEqualTo("Started"))
.log(LoggingLevel.INFO, "Starting route execution: " + routeId)
.to("controlbus:route?routeId=" + routeId + "&action=start&async=true")
.end();
}
routeDefinitionStart.end();
两条路线在白天停电1小时后停止,路线在1小时后自动开始。是不是因为JDK中的Timer组件导致了这个问题?
错误日志:
2017-11-05 01:01:13,921 INFO [route2] Stopping route execution: EventRoute
2017-11-05 01:01:13,921 INFO [route2] Stopping route execution: periodicMessageRoute
2017-11-05 01:01:26,671 INFO [org.apache.camel.impl.DefaultShutdownStrategy] Route: EventRoute shutdown complete, was consuming from: activemq://Queue.External?cacheLevelName=CACHE_CONSUMER&transacted=true
2017-11-05 01:01:26,672 INFO [org.apache.camel.impl.DefaultShutdownStrategy] Graceful shutdown of 1 routes completed in 22 seconds
2017-11-05 01:01:26,673 INFO [org.apache.camel.spring.SpringCamelContext] Route: EventRoute is stopped, was consuming from: activemq://Queue.External?cacheLevelName=CACHE_CONSUMER&transacted=true
2017-11-05 01:01:26,675 INFO [org.apache.camel.impl.DefaultShutdownStrategy] Starting to graceful shutdown 1 routes (timeout 300 seconds)
2017-11-05 01:01:26,676 INFO [org.apache.camel.impl.DefaultShutdownStrategy] Route: periodicMessageRoute shutdown complete, was consuming from: timer://monitor?fixedRate=true&period=30000
2017-11-05 01:01:26,680 INFO [org.apache.camel.impl.DefaultShutdownStrategy] Graceful shutdown of 1 routes completed in 0 seconds
2017-11-05 01:01:26,681 INFO [org.apache.camel.spring.SpringCamelContext] Route: periodicMessageRoute is stopped, was consuming from: timer://monitor?fixedRate=true&period=30000
2017-11-05 01:01:26,681 INFO [org.apache.camel.impl.DefaultShutdownStrategy] Starting to graceful shutdown 1 routes (timeout 300 seconds)
2017-11-05 01:01:26,681 INFO [org.apache.camel.impl.DefaultShutdownStrategy] Route: EventRoute shutdown complete, was consuming from: activemq://Queue.External?cacheLevelName=CACHE_CONSUMER&transacted=true
2017-11-05 01:01:26,681 INFO [org.apache.camel.impl.DefaultShutdownStrategy] Graceful shutdown of 1 routes completed in 0 seconds
2017-11-05 01:00:03,921 INFO [route3] Starting route execution: EventRoute
2017-11-05 01:00:03,924 INFO [route3] Starting route execution: periodicMessageRoute
2017-11-05 01:00:03,943 INFO [org.apache.camel.spring.SpringCamelContext] Route: EventRoute started and consuming from: activemq://Queue.External?cacheLevelName=CACHE_CONSUMER&transacted=true