Apache Camel – Polling GitHub commit

Hello everyone, In this post, you will learn How to Poll GitHub Commit using Apache Camel. The example has been tested and shared in the post.

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());
		}

	}
}
</repo_owner_user_name></branch_name></repo_name></your_password></your_use_name>