Skip to content

Instantly share code, notes, and snippets.

@welshstew
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save welshstew/0c82a84a9c189fa20a05 to your computer and use it in GitHub Desktop.
Save welshstew/0c82a84a9c189fa20a05 to your computer and use it in GitHub Desktop.
sends requests via stdin and files to a jms queue using camel. Log the response.
@Grab(group = 'org.apache.activemq', module = 'activemq-all', version='5.8.0')
@Grab(group = 'org.apache.camel', module = 'camel-core', version = '2.12.3')
@Grab(group = 'org.apache.camel', module = 'camel-jms', version = '2.12.3')
import org.apache.activemq.ActiveMQConnectionFactory
import org.apache.camel.ExchangePattern
import org.apache.camel.builder.RouteBuilder
import org.apache.camel.component.jms.JmsComponent
import org.apache.camel.impl.DefaultCamelContext
import org.apache.camel.impl.SimpleRegistry
/**
* Groovy script to start a camel context which will:
* 1. Respond to requests made over JMS to queue:the.soapoverjms.service
*/
def static setupRespondingJmsCamel() {
def brokerUrl = 'tcp://localhost:61616'
def queueUri = 'jms:queue:the.soapoverjms.service'
def jms = new JmsComponent(connectionFactory: new ActiveMQConnectionFactory(brokerURL: brokerUrl), useMessageIDAsCorrelationID: true)
def camelCtx = new DefaultCamelContext(new SimpleRegistry(['jms':jms]))
camelCtx.addRoutes(new RouteBuilder() {
def void configure() {
from(queueUri).setExchangePattern(ExchangePattern.InOut)
.to('log:gotmessage?showAll=true')
.setBody(constant('Goodbye World!'))
}
})
camelCtx.start()
// Stop Camel when the JVM is shut down
Runtime.runtime.addShutdownHook({ ->
camelCtx.stop()
})
synchronized(this){ this.wait() }
}
setupRespondingJmsCamel()
@Grab(group = 'org.apache.activemq', module = 'activemq-all', version = '5.8.0')
@Grab(group = 'org.apache.camel', module = 'camel-core', version = '2.12.3')
@Grab(group = 'org.apache.camel', module = 'camel-jms', version = '2.12.3')
@Grab(group = 'org.apache.camel', module = 'camel-stream', version = '2.12.3')
import org.apache.activemq.ActiveMQConnectionFactory
import org.apache.camel.builder.RouteBuilder
import org.apache.camel.component.jms.JmsComponent
import org.apache.camel.impl.DefaultCamelContext
import org.apache.camel.impl.SimpleRegistry
/**
* Groovy script to start a camel context which will:
* 1. Read in a file path and a header from stdinput
* 2. Send a message to a jms queue
*/
def static setupCamel() {
def brokerUrl = 'tcp://localhost:61616'
def queueUri = 'jms:queue:the.soapoverjms.service'
def jms = new JmsComponent(connectionFactory: new ActiveMQConnectionFactory(brokerURL: brokerUrl), useMessageIDAsCorrelationID: true)
def camelCtx = new DefaultCamelContext(new SimpleRegistry(['jms':jms]))
camelCtx.addRoutes(new RouteBuilder() {
def void configure() {
from('stream:in?promptMessage=Enter filepath to send <comma> soapHeader : ')
.process {
def (filePath, header) = it.in.getBody(String.class).tokenize(',')
it.in.headers.'SOAPJMS_soapAction' = header
it.in.body = new File(filePath).text
}
.to('log:hello?showAll=true').inOut(queueUri)
}
})
camelCtx.start()
// Stop Camel when the JVM is shut down
Runtime.runtime.addShutdownHook({ ->
camelCtx.stop()
})
synchronized(this){ this.wait() }
}
setupCamel()
2014-09-12 18:32:36,033 [main ] INFO DefaultCamelContext - Apache Camel 2.12.3 (CamelContext: camel-1) is starting
2014-09-12 18:32:36,034 [main ] INFO ManagedManagementStrategy - JMX is enabled
2014-09-12 18:32:36,172 [main ] INFO DefaultTypeConverter - Loaded 179 type converters
2014-09-12 18:32:36,362 [main ] INFO DefaultCamelContext - AllowUseOriginalMessage is enabled. If access to the original message is not needed, then its recommended to turn this option off as it may improve performance.
2014-09-12 18:32:36,362 [main ] INFO DefaultCamelContext - StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
2014-09-12 18:32:36,440 [main ] INFO DefaultCamelContext - Route: route1 started and consuming from: Endpoint[stream://in?promptMessage=Enter+filepath+to+send+%3Ccomma%3E+soapHeader+%3A+]
2014-09-12 18:32:36,447 [main ] INFO DefaultCamelContext - Total 1 routes, of which 1 is started.
2014-09-12 18:32:36,449 [main ] INFO DefaultCamelContext - Apache Camel 2.12.3 (CamelContext: camel-1) started in 0.415 seconds
Enter filepath to send <comma> soapHeader : /Users/swinchester/Downloads/sample-request.txt,hello
2014-09-12 18:33:15,525 [0 - stream://in] INFO myresponse - Exchange[Id: ID-Stuarts-MacBook-Pro-local-62549-1410510755528-0-2, ExchangePattern: InOnly, Properties: {CamelCreatedTimestamp=Fri Sep 12 18:33:15 EST 2014, CamelMessageHistory=[DefaultMessageHistory[routeId=route1, node=process1], DefaultMessageHistory[routeId=route1, node=to1]], CamelToEndpoint=log://hello?showAll=true}, Headers: {breadcrumbId=ID-Stuarts-MacBook-Pro-local-62549-1410510755528-0-1, CamelStreamComplete=true, CamelStreamIndex=0, SOAPJMS_soapAction=hello}, BodyType: String, Body: hello world, Out: null: ]
2014-09-12 18:33:15,551 [0 - stream://in] DEBUG TemporaryQueueReplyManager - Starting reply listener container on endpoint: Endpoint[jms://queue:the.soapoverjms.service]
2014-09-12 18:33:15,727 [verjms.service]] DEBUG TemporaryQueueReplyManager - Refreshed Temporary ReplyTo Queue. New queue: ID:Stuarts-MacBook-Pro.local-62569-1410510795582-1:1:1
2014-09-12 18:33:15,748 [verjms.service]] DEBUG TemporaryQueueReplyManager - Received reply message with correlationID [ID:Stuarts-MacBook-Pro.local-62569-1410510795582-1:2:1:1:1] -> ActiveMQTextMessage {commandId = 21, responseRequired = true, messageId = ID:Stuarts-MacBook-Pro.local-61349-1410509141837-1:3:1:4:1, originalDestination = null, originalTransactionId = null, producerId = ID:Stuarts-MacBook-Pro.local-61349-1410509141837-1:3:1:4, destination = temp-queue://ID:Stuarts-MacBook-Pro.local-62569-1410510795582-1:1:1, transactionId = null, expiration = 0, timestamp = 1410510795739, arrival = 0, brokerInTime = 1410510795739, brokerOutTime = 1410510795740, correlationId = ID:Stuarts-MacBook-Pro.local-62569-1410510795582-1:2:1:1:1, replyTo = temp-queue://ID:Stuarts-MacBook-Pro.local-62569-1410510795582-1:1:1, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = null, marshalledProperties = org.apache.activemq.util.ByteSequence@49a6dbe5, dataStructure = null, redeliveryCounter = 0, size = 0, properties = {breadcrumbId=ID-Stuarts-MacBook-Pro-local-62549-1410510755528-0-1, SOAPJMS_soapAction=hello, CamelStreamComplete=true, CamelJmsDeliveryMode=2, CamelStreamIndex=0}, readOnlyProperties = true, readOnlyBody = true, droppable = false, text = Goodbye Moon!}
2014-09-12 18:33:15,749 [verjms.service]] DEBUG TemporaryQueueReplyManager - Reply received. OUT message body set to reply payload: Goodbye Moon!
Enter filepath to send <comma> soapHeader :
#simple console logger to show the jms replies from jms request-response pattern using camel
log4j.rootLogger=INFO, out
# uncomment the following line to turn on Camel debugging
#log4j.logger.org.apache.camel=DEBUG
#turn on DEBUG logging to see jms replies
log4j.logger.org.apache.camel.component.jms.reply=DEBUG
# CONSOLE appender
log4j.appender.out=org.apache.log4j.ConsoleAppender
log4j.appender.out.layout=org.apache.log4j.PatternLayout
log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment