Wednesday, January 13, 2010

EJB3.0 Timer Service on Weblogic 10

Today I tried to develop and deploy EJB TimerService on OCSG4.1 which is in fact based on Weblogic10.

Below are my findings and there resolutions

EJB Code

@Stateless

public class TimerEjb implements TimerEjbLocal {

private Timer timer;

public TimerEjb() {

System.out.println("TimerEjb()");

}

@Resource

SessionContext context;

@Override

public void start() {

System.out.println("TimerEjb.start()");

timer = context.getTimerService().createTimer(10000, 50000, null);

}

@Override

public void stop() {

System.out.println("testejb.stop()");

timer.cancel();

}

@Timeout

public void TimerFired(Timer t) {

System.out.println("TimerEjb.check(Timer)");

}

}

Servlet Code

public class TimerServlet implements ServletContextListener {

@EJB(beanName = "TimerEjb")

TimerEjbLocal timerEjb;

@Override

public void contextDestroyed(ServletContextEvent sce) {

System.out.println("ServletContextListener.contextDestroyed()");

timerEjb.stop();

}

@Override

public void contextInitialized(ServletContextEvent sce) {

System.out.println("ServletContextListener.contextInitialized()");

timerEjb.start();

}

}

Following happens when I deployed this in clustered Weblogic

Initially I just deployed a timer on cluster which made timer event occurring on all managed servers, which is not acceptable because every event called twice.

Then i changed the weblogic-ejb-jar.xml to make it clustered by adding following line




<timer-implementation>Clustered</timer-implementation>

But now I see it complaining about no job scheduler data source is configured and it fails to deploy with following error trace

Following is the error trace

7' for task '38'. Error is: 'weblogic.application.ModuleException: Exception preparing module: EJBModule(TestEjbTimerEJB.jar)

Unable to deploy EJB: testejb from TestEjbTimerEJB.jar:

[EJB:011130]Error deploying EJB 'testejb(Application: TestEjbTimer, EJBComponent: TestEjbTimerEJB.jar)'. This EJB is configured to use the c

lustered EJB Timer implementation but the data-source-for-job-scheduler cluster attribute is not configured.

'

weblogic.application.ModuleException: Exception preparing module: EJBModule(TestEjbTimerEJB.jar)

Unable to deploy EJB: testejb from TestEjbTimerEJB.jar:

[EJB:011130]Error deploying EJB 'testejb(Application: TestEjbTimer, EJBComponent: TestEjbTimerEJB.jar)'. This EJB is configured to use the c

lustered EJB Timer implementation but the data-source-for-job-scheduler cluster attribute is not configured.

at weblogic.ejb.container.deployer.EJBModule.prepare(EJBModule.java:452)

at weblogic.application.internal.flow.ModuleListenerInvoker.prepare(ModuleListenerInvoker.java:93)

at weblogic.application.internal.flow.DeploymentCallbackFlow$1.next(DeploymentCallbackFlow.java:387)

at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:37)

at weblogic.application.internal.flow.DeploymentCallbackFlow.prepare(DeploymentCallbackFlow.java:58)

Truncated. see log file for complete stacktrace

[EJB:011130]Error deploying EJB 'testejb(Application: TestEjbTimer, EJBComponent: TestEjbTimerEJB.jar)'. This EJB is configured to use the c

lustered EJB Timer implementation but the data-source-for-job-scheduler cluster attribute is not configured.

at weblogic.ejb.container.deployer.BeanInfoImpl.validateClusteredTimerConfig(BeanInfoImpl.java:608)

at weblogic.ejb.container.deployer.BeanInfoImpl.prepareTimerService(BeanInfoImpl.java:584)

at weblogic.ejb.container.deployer.BeanInfoImpl.prepare(BeanInfoImpl.java:1001)

at weblogic.ejb.container.deployer.ClientDrivenBeanInfoImpl.prepare(ClientDrivenBeanInfoImpl.java:990)

at weblogic.ejb.container.deployer.Ejb3SessionBeanInfoImpl.prepare(Ejb3SessionBeanInfoImpl.java:297)

Truncated. see log file for complete stacktrace

>

To configure clustered aware ejb timer following section outlines the basic procedures for configuring your WebLogic Server environment.

Database Configuration

In order to maintain persistence and make timers cluster aware, Job Schedulers require a database connection.

For convenience, you can use the same database used for session persistence, server migration, etc.

In the database, you must create a table called WEBLOGIC_TIMERS. Schemas for creating this table in supported databases are located in:

WL_HOME/server/db/dbname/scheduler.ddl

where dbname is the name of the database vendor.

Data Source Configuration

After you have created a table with the required schema, you must define a data source that is referenced from within the cluster configuration. Job Scheduler functionality is only available if a valid data source is defined in for the DataSourceForJobScheduler attribute of the ClusterMbean. You can configure this from the WebLogic Server Administration Console.



Leasing

Leasing must be enabled for Job Schedulers. You can use either high-availability database leasing or non-database consensus leasing.

When using high-availability database leasing, you must create the leasing table in the database. Schemas for creating this table in supported databases are located in:

WL_HOME/server/db/dbname/leasing.ddl

where dbname is the name of the database vendor.

Now deploy your ear and you can see only one timer is active at a time.

One thing more when I first deploy EAR on I got start timer is negative, then while googling I found that initial timer should be greater then 0 while deploying on cluster.

timer = context.getTimerService().createTimer(10000, 50000, null);

No comments: