Apache Camel Plugin

  • Tags : web services, messaging, eip
  • Latest : 0.2
  • Last Updated: 26 March 2009
  • Grails version : *
  • Authors : Chris Navta
5 votes
Dependency :
compile ":camel:0.2"

Documentation

Summary

Installation

grails install-plugin camel

Description

This plugin has been superseded by the Grails Routing Plugin

Overview

The Apache Camel Grails plug-in allows you to send and route messages to a wide variety of destination endpoints directly from your Controllers and Services. It also provides a new Grails artifact, Routes, to configure your routes using know Enterprise Integration Patterns via the Apache Camel Java DSL.

Creating Routes

To create a new route, use the ‘grails create-route’ command:

grails create-route MyMessage

This will create a route in your ‘grails-app/routes’ directory:

class MyMessageRoute {
	def configure = {
	}
}

In the configure closure you have full access to the Camel Java DSL to configure your message routes.

Route Configuration

Simple Example

To create a route from an in-memory queue called “my.queue” to stdout, use:

from(“seda:my.queue”).to(“stream:out”)

This would print out any Object sent to “seda:my.queue” to the console.

Slightly More Complex Example

Suppose you wanted to send messages asynchronously to the following Grails Service:

class MyService {
	def myMethod(fooBarText) {
		log.info “Got text: ${ fooBarText }”
	}
}

Using Camel’s bean integration, we can deliver messages directly to any Grails Service:

from(“seda:my.queue”).filter {
	it.in.body.contains(“FooBar”)
}.to(“bean:myService?methodName=myMethod”)

This would deliver any message with the text “FooBar” in the body to the myMethod method of the myService service.

This example also illustrates one of the routing enhancements the plug-in offers. You can pass a Closure to the “filter”, “when” and “process” DSL methods.

Sending Messages

The plug-in provides a new method, ‘sendMessage’, to all Controllers and Services for sending messages to endpoints. It accepts a String endpoint and an Object message:

def myMessage = [name:”foo”,data:”bar”]
sendMessage(“seda:my.queue”,myMessage)

This would send the Map “myMessage” to an in-memory queue called “my.queue”.

Using Camel and ActiveMQ for JMS Messaging

Camel is a very easy way to integrate JMS messaging into your application. Just set up the ActiveMQ Component in your grails-app/conf/spring/resources.groovy:

beans = {
	activemq(org.apache.activemq.camel.component.ActiveMQComponent) {
		brokerURL = 'vm://MyBroker'
	}
}

Had to download: activemq-all-5.2.0.jar activemq-pool-5.2.0.jar

This will create an in-vm broker and configure the "activemq" uri:

sendMessage("activemq:my.queue","Message")

By default the component will create Queues from the uri. To create a Topic, add 'topic' to the URI:

sendMessage("activemq:topic:my.topic","Message")

Camel has rich support for JMS and ActiveMQ. To learn more about what options are available, check out the Camel documentation on the ActiveMQ and JMS Components.

Camel Components

Apache Camel has a wide variety of built-in Components for message delivery, such as JMS, SMTP, Web Services and Jabber. Take a look at the Apache Camel documentation for a comprehensive list.