14 setembro 2012

Customizing Camel components within Scala DSL

Even though I joined Oracle last July, I continue to have fun with some of other technologies, like the Scala language. And as I used to evangelize the Apache Camel framework in Brazil, I think I still owe some things to the Open Source community.

In a few weeks, I will be at JavaOne to talk about Apache Camel and the Camel Twitter component that I contributed, together with Brett Meyer, months ago. So while developing the demo, I found a small issue with the Camel Scala DSL that couldn't find anywhere else.

How to customize components within the Scala DSL for Apache Camel?
It happens that, using Java DSL, you can customize components simply like this:

public class TDCOnCamelRoute extends RouteBuilder {
@Override
public void configure() throws Exception {
String accessToken = System.getProperty("twitter.accessToken");
String accessTokenSecret = System.getProperty("twitter.accessTokenSecret");
String consumerKey = System.getProperty("twitter.consumerKey");
String consumerSecret = System.getProperty("twitter.consumerSecret");
TwitterComponent tc = new TwitterComponent();
tc.setAccessToken(accessToken);
tc.setAccessTokenSecret(accessTokenSecret);
tc.setConsumerKey(consumerKey);
tc.setConsumerSecret(consumerSecret);
getContext().addComponent("twitter", tc);
}
}
view raw gistfile1.java hosted with ❤ by GitHub
But using Scala DSL, does that really work?

class TDCOnCamelRoute extends RouteBuilder {
val accessToken = System.getProperty("twitter.accessToken")
val accessTokenSecret = System.getProperty("twitter.accessTokenSecret")
val consumerKey = System.getProperty("twitter.consumerKey")
val consumerSecret = System.getProperty("twitter.consumerSecret")
val tc = new TwitterComponent()
tc.setAccessToken(accessToken)
tc.setAccessTokenSecret(accessTokenSecret)
tc.setConsumerKey(consumerKey)
tc.setConsumerSecret(consumerSecret)
getContext.addComponent("twitter", tc)
}
view raw gistfile1.scala hosted with ❤ by GitHub
Not really... If you try this, the Camel Twitter component will throw an error saying that no token was provided for authentication. So here is how you make it work:

class TDCOnCamelRoute extends RouteBuilder {
val accessToken = System.getProperty("twitter.accessToken")
val accessTokenSecret = System.getProperty("twitter.accessTokenSecret")
val consumerKey = System.getProperty("twitter.consumerKey")
val consumerSecret = System.getProperty("twitter.consumerSecret")
val tc = new TwitterComponent()
tc.setAccessToken(accessToken)
tc.setAccessTokenSecret(accessTokenSecret)
tc.setConsumerKey(consumerKey)
tc.setConsumerSecret(consumerSecret)
override def onJavaBuilder(builder: org.apache.camel.builder.RouteBuilder) = {
builder.getContext().addComponent("twitter", tc)
}
}
view raw gistfile1.scala hosted with ❤ by GitHub
Hope this helps you some day! And you are welcome to attend my session "Leverage Enterprise Integration Patterns with Apache Camel and Twitter", at JavaOne 2012!

Nenhum comentário:

Contato

Email:bruno.borges(at)gmail.com

LinkedIn: www.linkedin.com/in/brunocborges
Twitter: www.twitter.com/brunoborges
Comprei e Não Vou
Rio de Janeiro, RJ Brasil
Oracle
São Paulo, SP Brasil