Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

Grape component allows you to fetch, load and manage additional jars when CamelContext is running. In practice with Camel Grape component you can add new components, data formats and beans to your CamelContext without the restart of the router. Grape component supports only producer endpoints.

Setting up class loader

Grape requires using Groovy class loader with the CamelContext. You can enable Groovy class loading on the existing Camel Context using the GrapeComponent#grapeCamelContext() method:

 

Code Block
languagejava
import static org.apache.camel.component.grape.GrapeComponent.grapeCamelContext;
...
CamelContext camelContext = grapeCamelContext(new DefaultCamelContext());

 You can also set up the Groovy class loader used be Camel context by yourself:

Code Block
languagejava
camelContext.setApplicationContextClassLoader(new GroovyClassLoader(myClassLoader));

URI format

Grape component supports only producer endpoints.

Code Block
grape:defaultMavenCoordinates[?options]

For example the following snippet loads Camel FTP component:

...

But of course Camel build-in type conversion API can perform the automatic data type transformations for you. In the example below Camel automatically converts binary payload into the String:

 

Code Block
languagejava
producerTemplate.sendBody("grape:defaultMavenCoordinates", "org.apache.camel/camel-ftp/2.15.2".getBytes());

...

In order to load the new component at the router runtime, just grab the jar containing the given component:

Code Block
languagejava
ProducerTemplate template = camelContext.createProducerTemplate();
template.sendBody("grape:grape", "org.apache.camel/camel-stream/2.15.2");
template.sendBody("stream:out", "msg");

...

In order to load the new processor bean  with your custom business login at the router runtime, just grab the jar containing the required bean:

Code Block
ProducerTemplate template = camelContext.createProducerTemplate();
template.sendBody("grape:grape", "com.example/my-business-processors/1.0");
int productId = 1;
int price = template.requestBody("bean:com.example.PricingBean?method=currentProductPrice", productId, int.class)

...

After you download new jar, you usually would like to have it loaded by the Camel again after the restart of the CamelContext. It is certainly possible, as Grape component keeps track of the jar files you have installed. In order to load again the installed jars on the context startup, use the GrapeEndpoint.loadPatches() method in your route: 

...

Code Block
languagejava
import static org.apache.camel.component.grape.GrapeEndpoint.loadPatches;

... 
camelContext.addRoutes(
  new RouteBuilder() { 
    @Override 
    public void configure() throws Exception { 
      loadPatches(camelContext);
 
      from("direct:loadCamelFTP").
        to("grape:org.apache.camel/camel-ftp/2.15.2"); 
    }
  });

 

Managing the installed jars

If you would like to check what jars have been installed into the given CamelContext, send message to the grape endpoint with the CamelGrapeCommand header set to GrapeCommand.listPatches:

Code Block
languagejava
      from("netty-http4:http://0.0.0.0:80/patches").
        setHeader(GrapeConstats.GRAPE_COMMAND, constant(CamelGrapeCommand.listPatches)).
        to("grape:list"); 

Connecting the to the route defined above using the HTTP client returns the list of the jars installed by Grape component:

Code Block
$ curl http://my-router.com/patches
grape:org.apache.camel/camel-ftp/2.15.2
grape:org.apache.camel/camel-jms/2.15.2

If you would like to remove the installed jars, so these won't be loaded again after the context restart, use the GrapeCommand.clearPatches command:

Code Block
languagejava
      from("netty-http4:http://0.0.0.0:80/patches").
        setHeader(GrapeConstats.GRAPE_COMMAND, constant(CamelGrapeCommand.clearPatches)).
        setBody().constant("Installed patches have been deleted."); 


Include Page
Endpoint See Also
Endpoint See Also