Question

DropWizard is a great framework for creating rest services, and camel is a great routing framework. However, it seems that these two ain't so easily integrated. For example, I would like to use the DropWizard way of creating rest endpoints, and then use camel to listen for the invocation of these services and route them accordingly.


There is a feature request for creating a dropwizard component within camel:

https://issues.apache.org/jira/browse/CAMEL-5961

However, this seems to be due some time in the future.


Another resource i found was the following github repository.

https://github.com/plasma147/dropwizard-camel-play

Which solves this by notifying camel of rest service calls through the use of the "direct" uri endpoint. However, one would have to manually hardcode this boilerplate into every rest service.


I had an idea that one could create a custom component and then use reflection to find all classes with the @Path annotation to register endpoints within camel, however i'm not sure if this would actually trigger. Or would it if I had a routing rule something along the lines of?:

from("dropwizard:/some/path").to(....)

Or am I forced to actually fire off the message from the rest service to camel.

Is there any better ways of integrating dropwizard and camel? Or is this it? i deas greatly appreciated.

Was it helpful?

Solution

Both ways are fine and doable, given you need to integrate another rest framework in Camel.

Two other REST components already exists that has taken different implementation approaches. You may want to study those to look for ideas.

The restlet component defines routes as from(restlet:http://host:port/some/path) which is a nice way to visualize rest endpoints if you intend to route them. It's easy to answer the question "what paths do exist in this app?" for someone looking at the code.

The CXFRS component is very similar to your other option, beans with @Path annotations. The CXF-RS component should be rather similar with a DropWizard component since they both implement the jax-rs specification. You can have a look at the implementation of the CXFRS component to get some ideas. I am aware that the cxf codebase in camel is rather comprahensive, with many features, but you should be able to grasp the concept from the code.

OTHER TIPS

Several months ago I made an attempt to integrate both. Since camel have plenty of options and have much bigger ecosystem than dropwizard I focused on most fundamental things - binding lifecycle together, bean registry and configuration properties.

You can take a look here: https://github.com/ConnectorIO/dropwizard-extensions/tree/master/camel

Dropwizard service configuration.

server:
logging:
properties:
  jms.from: ${JMS_FROM:-activemq:queue://.....}
  jms.broker: ${JMS_BROKER:-failover://tcp://127.0.0.1:61616}
  jms.username: ${JMS_USER:-nobody}
  jms.password: ${JMS_PASSWORD:-KnockKnock}

Example bundle:

  // ItemsConfiguration implements CamelConfiguration
private CamelBundle<ItemsConfiguration> camelBundle = new CamelBundle<>() {
  @Override
  protected Map<String, Object> getBeans(Map<String, Object> properties) {
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
      (String) properties.get("jms.username"),
      (String) properties.get("jms.password"),
      (String) properties.get("jms.broker")
    );
    connectionFactory.setTrustedPackages(Arrays.asList(
      ItemStateChangeDTO.class.getPackageName(),
      "java.util"
    ));

    ActiveMQComponent amq = new ActiveMQComponent();
    amq.setConnectionFactory(connectionFactory);
    return ImmutableMap.of("activemq", amq);
  }
};

Pushing routes to the context is then simple call:

camelBundle.getContext().addRoutes(new ItemConsumer("{{jms.from}}"));

Given that docs for module are non-existent feel free to take it away as an tip how to do own thing. You can use jersey/glassfish HK2 to inject producer/consumer templates into your services, hence there is no need to go any deeper into specifc DI mechanisms.

Feel free to report issues on github too. ;-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top