Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Groovy Example: ActiveMQ Broker and Apache Camel

DZone's Guide to

Groovy Example: ActiveMQ Broker and Apache Camel

· Java Zone
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

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/

 

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

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

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}