Apache Camel – Polling Github commit

Maven Dependencies

<dependency>
	<groupId>org.apache.camel</groupId>
	<artifactId>camel-core</artifactId>
	<version>${camel-version}</version>
</dependency>

<dependency>
	<groupId>org.apache.camel</groupId>
	<artifactId>camel-github</artifactId>
	<version>${camel-github-version}</version>
</dependency>

<dependency>
	<groupId>org.eclipse.mylyn.github</groupId>
	<artifactId>org.eclipse.egit.github.core</artifactId>
	<version>${github-core-version}</version>
</dependency>

Service Class

package com.dineshkrish;

import org.eclipse.egit.github.core.CommitUser;
import org.eclipse.egit.github.core.RepositoryCommit;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class GithubService {

	public void doSomething(RepositoryCommit commit) {
		
		CommitUser user = commit.getCommit().getCommitter();
		
		System.out.println("Hey you received new commit");
		System.out.println("Name: "+user.getName()+" Email: "+user.getEmail());
		
	}
	
}

Example Program

package com.dineshkrish.example8;

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class PollGithub {

	public static void main(String[] args) throws Exception {

		// creating the camel context
		CamelContext context = new DefaultCamelContext();

		try {

			// adding the routes to the camel
			context.addRoutes(new RouteBuilder() {

				@Override
				public void configure() throws Exception {

					from("github:commit?userName=<your_use_name>&password=<your_password>&repoName=<repo_name>&branchName=<branch_name>&repoOwner=<repo_owner_user_name>")
							.to("class:com.dineshkrish.GithubService?method=doSomething"); 

				}
			});

			while (true) {
				// start the context
				context.start();
			}

		} catch (Exception e) {
			// stop the context
			context.stop();
			System.out.println(e.getMessage());
		}

	}
}

Hello, folks, I am a founder of dineshkrish.com. I love Java and Open source technologies, If you find my tutorials are useful, please consider making donations to these charities.