The example utilizes the following technologies:
1) Coadunation, available from Coadunation.
2) Netbeans ID, available from Netbeans.
To implement distributed service delivery, there must be a service in this example a Coadunation Daemon and a mechanism of administering the service in this case a simple web application.
To start with, I implemented the basic daemon. This consists of a Daemon Interface definition, the Daemon Implementation and a simple XML Coadunation configuration file. (There is more information on this in the Coadunation Documentation.)
Creating the Daemon Project
Create a new project in Netbeans, using the following steps:
1) Click on new project button.
2) Select General project.
3) Select Java Class Library.
4) Click the Next button.
5) Enter a Project Name, "1001-BasicDaemon" (The numbers at the front indicate start order to Coadunation. This is similar to the Unix rc start order process.)
6) Enter a location to store the project.
7) Click the Finish button.
Once this has been done a basic Java Class Library project will be present in Netbeans. It is now simply a matter of defining the components of the daemon, starting with the Java interface.
Creating the Daemon Interface
Coadunation supports both local and remote interfaces, but unlike EJB's it is possible to use the same interface for both local and remote, the process of narrow is the only difference. Like EJB's a local interface is only available within the same jar. As this example requires that the web application is able to make calls on the daemon it must be both and not just a local interface.
To implement a remote interface, simply extend java.rmi.Remote, and adhere to the RMI interface standard.
Create a new interface file in the project by following these steps:
1) Right click on the source package and select "New" than "File/Folder".
2) Select "Java Classes" out of the "Categories".
3) Select "Java Interface" out of the "File Types".
4) Click the Next button.
5) Enter a Interface Name eg, "BasicDaemon".
6) Enter a package location eg, "org.example".
7) Click the Finish button.
Once this is completed a new Java Interface will be present in the editor panel. It should look something like the following.
/*
* BasicDaemon.java
*
* Created on October 30, 2007, 6:35 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package org.example;
/**
*
* @author brett
*/
public interface BasicDaemon {
}
Now convert the interface to a remote interface by extending java.rmi.Remote. The interface defintion should now look like the following:
public interface BasicDaemon extends java.rmi.Remote {
In this example the only thing left to do on the interface is the method definition. This method must adhere to RMI standards, this means that it must through java.rmi.RemoteException. The implementation need not but the definition must.
A simple method as follows will suffice for this example:
/**
* A simple hello world example
*/
public String helloWorld(String msg) throws java.rmi.RemoteException;
Creating the Daemon Implementation
Now that the Interface is defined it must be implemented by a Java class. Create a class using the following steps:
1) Right click on the source package and select "New" than "File/Folder".
2) Select "Java Classes" out of the "Categories".
3) Select "Java Class" out of the "File Types".
4) Click the Next button.
5) Enter a Class Name eg, "BasicDaemonImpl".
6) Enter a package location eg, "org.example".
7) Click the Finish button.
As with the Java Interface a Java Class should now be present in the editor and look like the following:
/*
* BasicDaemonImpl.java
*
* Created on October 30, 2007, 7:00 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package org.example;
/**
*
* @author brett
*/
public class BasicDaemonImpl {
/** Creates a new instance of BasicDaemonImpl */
public BasicDaemonImpl() {
}
}
The class must now be modified to implement the interface, this is done by using the Java "implements" key word, appending the name of the interface and by implementing the interface defined method. The class definition should look like the following once the interface has been implemented:
/**
*
* @author brett
*/
public class BasicDaemonImpl implements BasicDaemon {
/** Creates a new instance of BasicDaemonImpl */
public BasicDaemonImpl() {
}
/**
* A simple hello world example
*/
public String helloWorld(String msg) {
System.out.println(msg);
return "Hello world";
}
}
In the above example the helloWorld method prints out the "msg" string passed to it and returns the string value "Hello world".
Creating the Daemon Configuration
At this point in time the daemon is almost ready for deployment in Coadunation, all that is required is the configuration file. This file must be added to the META-INF directory within the library. To add the file follow these steps.
1) Right click on the source package and select "New" than "File/Folder".
2) Select "XML" out of the "Categories".
3) Select "XML Document" out of the "File Types".
4) Click the Next button.
5) Enter the "File Name" as "coadunation"
6) Enter the "Folder" as "src/META-INF".
7) Click the Next button.
8) Select "Well-Formed Document"
9) Click the Finish button.
Netbeans will now present an XML document that looks like the following:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Document : coadunation.xml.xml
Created on : October 30, 2007, 7:19 AM
Author : brett
Description:
Purpose of the document follows.
-->
<root>
</root>
This document will have to be modified to look like the following:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Document : coadunation.xml
Created on :
Author : Author
Description:
This is a basic daemon example
-->
<coadunation version="1" name="BasicDaemon">
<beans>
<bean>
<interface>org.example.BasicDaemon</interface>
<class>org.example.BasicDaemonImpl</class>
<bindname>BasicDaemon</bindname>
<role>daemon</role>
</bean>
</beans>
</coadunation>
The section in the xml are broken down as follows:
name: The name of the Daemon.
interface: This is the Java interface that will be exposed by the daemon.
class: This is the Java class that implements the interface.
bindname: This is the name that will be bound to the JNDI.
role: The security role required to access this daemon.
(For more information on the Coadunation configuration file consult the Coadunation Documentation.)
Deploying the Daemon in Coadunation
It is now simply a matter of deploying the daemon in Coadunation. This requires a running instance of Coadunation. Which can be downloaded from the coadunation web site. It is available in either an install jar or source code.
Once Coadunation has been installed it can be started by running the "run.sh" (On unix) or "run.bat" (On windows). This script is found in the bin directory of the Coadunation install.
It is now simply a matter of copying the compiled jar from the Netbeans project into the deploy directory of the Coadunation install. Once this is down Coadunation will auto deploy the daemon.
To compile the project in Netbeans right click on project name and select "Build Project". Once this is done a dist directory will be create in the project folder. Within this directory is the compiled jar for the project.
End of Part 2
This ends part 2. For more information visit the Coadunation web site.
I will explain the web application implementation in Part 3.
No comments:
Post a Comment