Friday, January 13, 2006

Presentation - work in progress

Yesterday I've started to write my presentation concerning EJB3. It covers more then just this - it's about whole Java EE 5 technologies including JAX WS, servlets, JSP, Common annotaton etc. Actually I have six pages and working examples.
Basically I can say - EJB3 is all about annotations. But annotations are processed by application server or application client so in the end, on background it works similary to EJB 2.1. The main difference is in development point of view. You, as developer, don't need to care whenever you have all interfaces and stub classes, everything is done on deployment. Is very interesting to see what was done with our project after deployment. Just take a closer look to deployment directory and you'll see, that nothing important has changed.
So stop just talking and watch this example:
package com.blogspot.deftech.ejb3;
@javax.ejb.Stateless (name="ejb/foo")
public class FooBean {
public String getString (String stringIn) {
return "You said " + stringIn + " ... Unbelievable !";
}
}
For such class we need an interface - lets create it (nothing complicated)
package com.blogspot.deftech.ejb3;
@javax.ejb.Remote
public interface Foo {
public String getString (String stringIn);
}

As you can see, to create ejb is very very simple. Of course, this one is the simpliest one, but the basic concept is clear. Use annotation and don't care. Everything else will be done by deployer or server itself. In other words - concentrate on business logic, not on support stuff.
To generate server-side client is easy as one two three :-). Look at this piece of code:
package com.blogspot.deftech.ejb3;
public class FooTest {
@javax.ejb.EJB (name="ejb/foo")
private Foo fooObject;

public static void main (String [] args) {
FooTest test = new FooTest();
test.run();
}

private void run () {
System.out.println(fooObject.getString ("1+1=2"));
}
}
To deploy such code you need to compile it and pack it into *.jar file. More on this later today. Be patient ...
Ok this one will not work, but if you make a servlet based on this skeleton, you'll see, how application server literaly injects instance of class Foo to member variable fooObject. There is even another trick like. Let's examine this example:
package com.blogspot.deftech.ejb3;

public class ResourceExample {
javax.sql.DataSource ds = null;

@Resource(name="jdbc/OracleXE")
public void setDataSource javax.sql.DataSource value) {
ds = value;
}
}
In this piece of code application server will inject JNDI resource into setter named setDataSource. Really clean and elegant way how to work with JNDI. No more JNDI lookups, no more narrowing references. But still keep in mind, that on background nothing changes ! The only thing which has changed is that the boring part of work is done by server ... Later I'll post here example of entity beans, which are even more interesting. And also a small example of Web service. But don't expect anything revolutionary - just another set of annotations :-).

No comments: