Saturday February 25, 2006
Executing scheduled task by MBeans in Glassfish In last blog the implementation of cron was described. I used lifecycle module and Timer bean in this
approach. This solution was used since lifecycle listeners (similar as you know in web module) are missing
for EJB module. It seems as lack of the specification. Therefore, server's vendors used some proprietary
solutions like WebLogic defines some lifecycle methods in server specific deployment descriptors. Sankara suggested to use MBean with predefined Timer rules. Therefore, I decided to implement this task by MBean. We can use Netbeans plugin for JMX that is avalaible on update center and tutorial is here.
Now, let's create new J2SE project in Netbeans and create CronMB interface that tipically consists of named and typed attributes that can be read, written, and named typed operations that can be invoked, e.g. stop cron, change action, ... Then, create new class CronMBean class that implements this and javax.management.NotificationListener interfaces. Add business logic in handleNotification method. Now, build your module.
Logg in web admin console, select Custom MBeans node, click Deploy button and choose location of the jar with your cron MBean. Click Next and specify MBean class name. Now, we should create action that occurs based on timer action. Select Management Rules node and click Nwe button. Specify name and select timer as event type. In next panel specify additional info related to this timer and assign your MBean to the action.

Faster deployment to Glassfish I meet Jerome Dochez a two weeks ago and complained slow deployment to Glassfish. Our J2EE application that is developed in our team as referential application with EJB 3.0 has about ten entity and session beans. Deployment this application with one EJB and one web module took about 40 sec. I think that it's too much. Jerome suggested me to try to disable security manager in Glassfish. Therefore, I commented out following line with jvm-options in domain.xml file:
<jvm-options>-Djava.security.policy=${com.sun.aas.instanceRoot}/config/server.policy</jvm-options>
Then restarted server and deployed application. The deployment took 25 sec. It means deployment time was decreased by 37 percent. I think that doesn't make a sense use security manager on testing servers and I guess that many users don't need this feature. This security feature can be setup during server's installation. There might be a panel where user can select which type of server's configuration he wants: production or development and when he selects development mode the security manager can be disabled and logging can be switched to higher level, ...
Posted by pblaha
( Feb 22 2006, 02:21:57 PM CET )
Permalink
Comments [1]
How to implement cron in Enterprise application One my colleague asked me for implementing cron functionality in J2EE applicationthat is deployed in Sun application server. He needs to execute scheduled commandsin his EJB module. I suggested folowing solution that consists of Timer bean and lifecycle module that starts the timer when the server is started.
I know that better solution is used lifecycle module that is connected to EJB module only. For instance, where the module is deployed the lifecycle listener can invoke the bean. However, Sun
Application server doesn't support this type lifecycle listener. The WebLogic server supports lifecycle listeneres on module application level, see here. Other solution is use servlet life cycle listener in web tier but this suggestion adds additional dependency for J2EE application. Since I didn't find better solution I used lifecycle module and Timer bean. Steps are below:
public void startTimer() {
TimerService timerServ = context.getTimerService();
// create interval timer
Timer timer = timerServ.createTimer(new Date(), 5000, "Timer");
}
public void handleEvent(LifecycleEvent lifecycleEvent)
throws ServerLifecycleException {
// start timer when server is ready to service requests
if(lifecycleEvent.getEventType() == LifecycleEvent.READY_EVENT){
LifecycleEventContext lfcECtx = lifecycleEvent.getLifecycleEventContext();
try {
lfcECtx.log("Try to lookup Timer's bean home: " + EJB_NAME);
InitialContext ctx =
lifecycleEvent.getLifecycleEventContext().getInitialContext();
Object obj = ctx.lookup(EJB_NAME);
TimerSessionRemote timer =
((TimerSessionRemoteHome)PortableRemoteObject.narrow(obj,
TimerSessionRemoteHome.class)).create();
lfcECtx.log("Create Timer bean");
timer.createTimer();Where is the NetBeans with JavaEE5 support avalaible? I have seen some e-mails on mailing lists that NetBeans JavaEE daily builds aren't avalaible on netbeans.org. It's true and this issue should be already resolved. However, forget for Java EE 5 builds since these builds will not be avalaible any more, you should use 5.5 release from now.

How to use date in persistence In today's post I would like to describe how to use date data type in persistence class and then in queries. We very often use java.util.Date or java.util.Calendar objects in Java, these types can be used in persistence as well. In persistence class you can use Temporal annotation for specifying how the date property should be persisted. Without using Temporal annotation the property is persisted as TIMESTAMP. The TemporalType defines the mapping for temporal type, i.e. DATE, TIME, TIMESTAMP. The example is below:
@Temporal(TemporalType.TIME)
public Date getDueTime(){
return dueDate;
}
In this example dueDate will be persisted as java.sql.Date. Now, this property will be used in query. We should use setParameter in Entitymanager class and specify TemporalType for query like:
ListPosted by pblaha ( Feb 15 2006, 09:19:49 AM CET ) PermalinkcurrentPlans = em.createQuery("SELECT a FROM Plan a WHERE a.creationTime <= :time"). setParameter("time", new Date(), TemporalType.TIME).getResultList();
New code completion for Entity class in NetBeans The new preview release of the NetBeans 5.5 will be released very soon. This new version is focused on support for Java EE5 especially. This changes require new features in code completion as well. We plan to support code completion for almost all annotations for persistance. For instance, you will be able to see tables that are in database that is specified in persistence file and the connection is registered in NetBeans. See snapshot:

Using enumeration in Entity class In 5.0, the Java programming language gets support for enumerated types. In today's post I would like to show how to use enum in Entity classes. We should create new enumerated type firts:
public enum AdvertisementState { OPEN, CLOSE, RESRVED}
This enum represents status of some advertisement. Then, we can create Entity class that represents advertisement:
@Entity()
public class Advertisement implements Serializable {
private AdvertisementState state;
@Enumerated(EnumType.ORDINAL)
public AdvertisementState getState() {
return state;
}
You can use ORDINAL or STRING type. When you use ORDINAL type then columnt type is used INTEGER and for STRING the VARCHAR is used. Now, we can use the enum type for finding advertisements with OPEN status:
em.createQuery("SELECT a FROM Advertisement a WHERE a.state = :state").
setParameter("state", AdvertisementState.OPEN).
setMaxResults(count).getResultList();
Using enum type is very easy, isn't it?
Posted by pblaha
( Feb 08 2006, 12:08:20 AM CET )
Permalink