Sending a Tweet Using Apache Camel


Twitter is an online news and social networking service where users post and interact with messages.

POC on sending a tweet from java application using Apache camel API

Technologies used in this POC are :
1. JDK 1.7
2. Apache Camel 2.12.x
3. Maven.

Lets create a maven project.

open pom.xml add the below dependencies


 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
      <modelVersion>4.0.0</modelVersion>  
      <groupId>com.learning</groupId>  
      <artifactId>tweetingthroughapachecamel</artifactId>  
      <version>0.0.1-SNAPSHOT</version>  
      <dependencies>  
           <dependency>  
                <groupId>org.apache.camel</groupId>  
                <artifactId>camel-core</artifactId>  
                <version>2.12.0</version>  
           </dependency>  
           <dependency>  
                <groupId>org.apache.camel</groupId>  
                <artifactId>camel-twitter</artifactId>  
                <version>2.12.0</version>  
           </dependency>  
      </dependencies>  
 </project>  

Generate the ConsumerSecret,ConsumerKey,AccessToken and AccesstokenSecret from https://apps.twitter.com/

Now lets create a RouteBuilder JavatoTwitterRouteBuilder.java class

 package com.learning.apachecamel.twitter;  
 import org.apache.camel.builder.RouteBuilder;  
 public class JavatoTwitterRouteBuilder extends RouteBuilder {  
      @Override  
      public void configure() throws Exception {  
           // Generate the ConsumerSecret,ConsumerKey,AccessToken and  
           // AccesstokenSecret from https://apps.twitter.com/  
           from("direct:sendingtweet").to(  
                     "twitter://timeline/user?consumerKey=XYZ&consumerSecret=ZYZ&accessToken=ZYZ&accessTokenSecret=MAY");  
      }  
 }  
Lets create one more class to test the routes.


 package com.learning.apachecamel.twitter;  
 import org.apache.camel.CamelContext;  
 import org.apache.camel.ProducerTemplate;  
 import org.apache.camel.impl.DefaultCamelContext;  
 public class JavaTwittingClass {  
      public static void main(String[] args) {  
           CamelContext camelContext = new DefaultCamelContext();  
           try {  
                camelContext.addRoutes(new JavatoTwitterRouteBuilder());  
                ProducerTemplate producerTemplate=camelContext.createProducerTemplate();  
                camelContext.start();  
                producerTemplate.sendBody("direct:sendingtweet","This tweet is done from Java Application");  
                System.out.println("Message sent");  
           } catch (Exception e) {  
                System.err.println("Message not sent");  
                e.printStackTrace();  
           }  
      }  
 }  


Output:
   
 SLF4J: Defaulting to no-operation (NOP) logger implementation  
 SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.  
 Message sent  


I am an open source enthusiast. Currently having 6 years of experience and part of Ericsson Global. I am a traveler, Coding manic, Foodie, Gaming Freak and an Amazing Cook.

Share this

Pages
Previous
Next Post »