Groovy example: ActiveMQ broker and Apache Camel

I’m still playing around with groovy scripting. It’s an excellent way to quickly prototype some ESB scenarios. Last week 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()
blog comments powered by Disqus