Over a million developers have joined DZone.

Groovy Example: ActiveMQ Broker and Apache Camel

· Java Zone

Navigate the Maze of the End-User Experience and pick up this APM Essential guide, brought to you in partnership with CA Technologies

I’m playing around with Groovy scripting. It’s an excellent way to quickly prototype some ESB scenarios.

Recently I blogged about using Groovy to write files to a gtalk account using Apache Camel.

The example below shows you how to start an ActiveMQ broker, which persists messages to a PostgreSQL database.

An Apache Camel route is create to test the ActiveMQ queue.

 

#!/home/akoelewijn/programs/groovy-1.6.0/bin/groovy

import org.apache.camel.impl.DefaultCamelContext
import org.apache.camel.language.groovy.GroovyRouteBuilder
import org.apache.activemq.camel.component.ActiveMQComponent

@Grab(group='org.apache.camel', module='camel-groovy', version='1.6.0')
@Grab(group='org.apache.camel', module='camel-jms', version='1.6.0')
@Grab(group='org.apache.activemq',module='activemq-core',version='5.2.0')
@Grab(group='org.apache.activemq',module='activemq-camel',version='5.2.0')
@Grab(group='org.apache.camel', module='camel-core', version='1.6.0')
@Grab(group='postgresql', module='postgresql', version='8.3-603.jdbc4')
@Grab(group='commons-dbcp', module='commons-dbcp', version='1.2.2')
class SampleRoute extends GroovyRouteBuilder {
protected void configure(){
from("file:///tmp/from").
to("activemq:queue:q1")
from("activemq:queue:q1").
to("file:///tmp/to")
}
}

def ds = new org.apache.commons.dbcp.BasicDataSource()
ds.setDriverClassName("org.postgresql.Driver")
ds.setUrl("jdbc:postgresql://localhost/dev1")
ds.setUsername("rgw_owner")
ds.setPassword("rgw_owner")

def pa = new org.apache.activemq.store.jdbc.JDBCPersistenceAdapter()
pa.setDataSource(ds)
pa.setAdapter(new org.apache.activemq.store.jdbc.adapter.PostgresqlJDBCAdapter())

def brokerSvc = new org.apache.activemq.broker.BrokerService()
brokerSvc.setBrokerName("q1")
brokerSvc.addConnector("tcp://localhost:61616")
brokerSvc.setPersistenceAdapter(pa)
brokerSvc.start()

def camelCtx = new DefaultCamelContext()
camelCtx.addComponent("activemq", ActiveMQComponent.activeMQComponent("tcp://localhost:61616"));
camelCtx.addRoutes(new SampleRoute())
camelCtx.start()

From http://www.andrejkoelewijn.com/wp/

 

Thrive in the application economy with an APM model that is strategic. Be E.P.I.C. with CA APM.  Brought to you in partnership with CA Technologies.

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}