Andrej Koelewijn is an IT Architect for IT-eye in the Netherlands. His current focus is on Open Source Java, SOA and web projects. Andrej also has a lot of experience using Oracle java and database technologies. Andrej enjoys speaking about software and technology at seminars, and giving guest lectures at universities. You can read his blog at http://www.andrejkoelewijn.com/ Andrej is a DZone MVB and is not an employee of DZone and has posted 2 posts at DZone. View Full User Profile

Groovy Example: ActiveMQ Broker and Apache Camel

03.16.2009
| 21587 views |
  • submit to reddit

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/

 

Published at DZone with permission of Andrej Koelewijn, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)