Friday, March 21, 2003

CodeBit: The Simplest Possible EJB for JBoss?

I've been trying to simplify the process of making an EJB. And I think I have the simplest possible bean. And a simple client program. I've packaged this all rather neatly into a zip file so that you can instantly create an Eclipse project. Here is the zip file: http://affy.blogspot.com/java/SimpleBean.zip

SimpleBean.java

Make sure to get your JBoss server started. Then customize the build.xml file for your configuration (mostly just the first 10 lines or so). Then open a DOS window and run ant deploy. After the Ant process is done, run the SimpleBeanClient class from Eclipse.

Let me know if you have any problems.

package com.affy.entity;

import java.rmi.RemoteException;
import javax.ejb.*;
import com.affy.util.SimpleBeanUtil;

/**
 * @ejb.bean name="SimpleBean" type="CMP" cmp-version="2.x" jndi-name="ejb/affy/SimpleBean" primkey-field="pKey"
 * @ejb.util generate="physical"
 */
public abstract class SimpleBean implements EntityBean {

	public EntityContext mContext;

	/** @ejb.persistence */
	public abstract String getPKey();
	public abstract void setPKey(String pKey);

	/** @ejb.create-method view-type="remote" */
	public String ejbCreate() throws EJBException, CreateException {
		this.setPKey(SimpleBeanUtil.generateGUID(this));
		return this.getPKey();
	}

	public void ejbPostCreate() { /* do nothing */ }
	public void ejbRemove() throws RemoveException, EJBException, RemoteException {}
	public void ejbActivate() throws EJBException, RemoteException {}
	public void ejbPassivate() throws EJBException, RemoteException {}
	public void ejbLoad() throws EJBException, RemoteException {}
	public void ejbStore() throws EJBException, RemoteException {}
	public void setEntityContext(EntityContext lContext) { mContext = lContext; }
	public void unsetEntityContext() { mContext = null; }
}

SimpleClient.java

package com.affy.client;

import com.affy.remote.SimpleBean;
import com.affy.home.SimpleBeanHome;
import com.affy.util.SimpleBeanUtil;

class SimpleBeanClient {

	public static void main(String[] args) {
		System.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
		System.setProperty("java.naming.provider.url", "localhost:1099");
	
		try {
			SimpleBeanHome home = SimpleBeanUtil.getHome();

			SimpleBean sb = home.create();
			String pKey = (String)sb.getPrimaryKey();
			System.out.println("ID: " + pKey);

			SimpleBean b = home.findByPrimaryKey(pKey);
			System.out.println("ID: " + b.getPrimaryKey());

			System.out.println("Done.");
		} catch(Exception e) {
			System.out.println(e.toString());
		}
	}
}
Post a Comment