Monday, June 20, 2011

Configuring Apache Camel (Part I)

So, what's all the hype about Apache Camel? You might most probably have heard/read that (according to Wiki), it's a rule-based routing and mediation engine which blah blah blah... The term to note here is "mediation engine". Basically, it is an open source framework that you can build on top for integrating applications, or in lesser words, a middleware framework.

It supports extension to other frameworks, such as Apache CXF, ActiveMQ, Jetty, Quartz and so much more, which makes your app highly 'talkative'. For the full list of components, you can always refer to the site here.

Camel has the concept of endpoints, routes, consumer, producers and processors. Endpoints are services you've defined to listen or to send out messages. Routes on the other hand are consist of endpoints and processors. Endpoints, for instance, are A and B, a Route is (A->B). - Thanks MK for the explanation.

There are two ways that I know of to start writing your own apps running on Camel. First, you need to know what are your endpoints, and the route of the messages coming in. Next, decide if your application needs to add new routes or endpoints during runtime, or routing rules are fixed after startup. Do you need to allow your application to be deployed on top of a web container?

More to come...

Friday, June 17, 2011

Apache Camel


I would like to write something about this, but don't know where to start. Maybe I should start by saying, I like camel.

More to come in my next post - Configuring Apache Camel.

Friday, June 10, 2011

Java Native Interface

The JNI API allows your Java application to talk to other applications written in C/C++. So today I'll show an example on how to get your Java class talk to a C program. I'll briefly describe the steps below.

First, create a Java file (I'll name our class TestJNI).
class TestJNI {

public static void main(final String[] args) {
TestJNI test = new TestJNI();
test.printText();
}

public native void printText();
}
Compile the source above to create a class file.
> javac TestJNI.java
This will generate the class file TestJNI.class. The method you need to take note here is the public native void printText() method. This is the method that we are calling in the program written in C. The native modifier tells the compiler that when this method is invoked call the C program.

Once you have the class file, the next step is to generate the C/C++ header file with the javah command. You can find the javah command in your JDK bin directory.
> javahTestJNI
A new file will be generated. We now have three files,
  • TestJNI.java
  • TestJNI.class
  • TestJNI.h
In this example, I am using Microsoft Visual C++ 2010 Express. You can get it off Microsoft site and you need to obtain the registration key to activate the license. Once you got it running, create a new empty Win32 Project project.

Include the JNI header files into the project. Right click on "Project Name" > Properties > Configuration Properties > VC++ Directories > Include Directories. You need to include all the directories for the JNI header files, which is located at ${JAVA_HOME}/include and ${JAVA_HOME}/include/win32. Include also the directory where your TestJNI.h file is located in this step.

Next, add TestJNI.h file to your project. Create a .cpp file. In this example, I am naming it Entry.cpp. Open TestJNI.h with any editor. Copy the bolded section to Entry.cpp.
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class TestJNI */

#ifndef _Included_TestJNI
#define _Included_TestJNI
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: TestJNI
* Method: printText
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_TestJNI_printText
(JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif
Your Entry.cpp should have the following.
#include
#include "TestJNI.h"

JNIEXPORT void JNICALL Java_TestJNI_printText(JNIEnv *, jobject) {
printf("HELLO WORLD");
}
Build the solution and get the .dll file generated. The name of the dll file is your project name. You should be able to find it somewhere off the debug folder of your project. Next, we need to include this in your Java program using the System.load command.

System.load("c:/path/to/dll/TestDll.dll");
Your Java application should print HELLO WORLD. Hope that helps.


Thursday, June 9, 2011

New Post

This is my first post on blogspot. For my older posts about coding, you can refer to my WordPress blog, Perspective. You can find the link at the Unusual Links corner.

I'll update more freaky codes as time goes by. Cheers.

Share it