Now that we have all the preliminary setup of the environment we next need to create a session bean.
The Session Bean
The basic requirement for a session bean is that it must implement an interface and be either annotated with @Stateless or be registered in an XML file. We are sticking strictly to using annotations. The annotation goes on the class and not the interface, so here’s the interface.
The Interface
First we create a session bean. Here is one such example:
To create this file,
- select your src directory, right-click and select New:Interface.
- For Name, enter HelloWorldService
- For Package enter service
- Click on Finish then enter the above code into the file
- Save your file (ctrl-s)
The Session Bean
Next, we need to create a session bean. Here’s the code for it:
Notice that this class has the @Stateless annotation. The container will find this class and register it automatically under JNDI using the (non-package qualifited) class name plus “/local”. In this example, that means we’ll need to lookup “HelloWorldServiceImpl/local”.
This class is obviously stateless because of the annotation. This is the default behavior. However, using the annotation will get it automatically available from JNDI. (We could put this information in an XML file and get the same results.)
This class is also local. By default, session beans are local (no RMI-IIOP to call them) unless:
- They implement more that one interface (ignoring common interfaces like Serializable and Comparable).
- There is a @Remote annotation
If you still want a local session bean where there is more than one interface, you can use @Local.
To create this file:
- select your src directory, right-click and select New:Class
- For Name, enter HelloWorldServiceImpl
- For Package enter service.impl
- Click on Finish then enter the above code into the file
- Save your file (ctrl-s)
Comments