9 Replies Latest reply on Jul 26, 2006 2:10 PM by Eric Anderson

# Call EJB on a Schedule?

Hello everyone,

is it possible to configure JBoss 4.0.4 to call a method on a stateless session bean (or alternatively send a message to a message bean) on a schedule (e.g. once per day)?

Regards,

Andreas

• ###### 1. Re: Call EJB on a Schedule?

Yes,
you should use a Timer.

implement TimerObject in you StatelessSessionBean, EntityBean or MessageBean.
In your application set a Timer for this Bean.
After that the timout method of your bean is called when the timeout has come ....

The Timer is persistent (since 4.0.4 also if you shutdown the JBoss) as long as the application delete it.

For more info see sun or jboss tutorials or some books ;-)

Wolf

• ###### 2. Re: Call EJB on a Schedule?

Thanks for your help. I have now implemented the TimedObject interface in my bean.

The problem is now how to register the Timer. I'd like the (recurring) Timer to be run right from application startup, before I invoke any of the component interface methods. Is this possible? Ideally, I'd like to configure the Timer somewhere in the Container configuration, not within the bean itself.

Best regards,

Andreas

• ###### 3. Re: Call EJB on a Schedule?

often we have two cases:

- do something for the depends on a timestamp from data
(e.g. warning of a meeting in calendar)

- start some checks for administrativ issues
(cleaning or statistics ...)

In the first case the entity manage the timer (it's easy)

In the second case an administrator manage it.
I create the Timer with a client (WEB or rich) with an administration console. But I think you can do it whit an mbean for JBoss as well.

• ###### 4. Re: Call EJB on a Schedule?

"wdfink" wrote:

- start some checks for administrativ issues (cleaning or statistics ...)
In the second case an administrator manage it.

This is the case in my app.

"wdfink" wrote:

I create the Timer with a client (WEB or rich) with an administration console. But I think you can do it whit an mbean for JBoss as well.

Isn't there a place in the jboss configuration files to configure Timers? Where could I find information on writing MBeans that do the trick?

Best regards,

Andreas

• ###### 5. Re: Call EJB on a Schedule?

Timers are an EJB 2.1 feature so the application has to manage.

MBeans are helpful to do something server/system specific for the EJB's.

A point to start for MBeans are JBoss admin guide (http://docs.jboss.org/jbossas/jboss4guide/r5/html/) chapter '2.4.3. Writing JBoss MBean Services'

• ###### 6. Re: Call EJB on a Schedule?

You can use quartz instead of a Timer in you MBean.

For using quartz you need two file :
in $JBOSS_HOME\server\default\lib\quartz-all-1.5.2.jar org\quartz\ee\jmx\jboss\doc-files\quartz-service.xml-example --> ..\deploy\quartz-service.xml And you Quartz DS definition file: quartz-ds.xml <?xml version="1.0" encoding="UTF-8"?> <!--$Id: mysql-ds.xml,v 1.3.2.1 2004/12/01 11:46:00 schrouf Exp \$ -->
<!-- Datasource config for MySQL using 3.0.9 available from:
-->

<datasources>
<local-tx-datasource>
<jndi-name>QuartzDS</jndi-name>
<connection-url>jdbc:mysql://xxxx:3306/quartz</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>quatrzUser</user-name>
<!--
-->
<new-connection-sql>SELECT CURDATE();</new-connection-sql>
<check-valid-connection-sql>SELECT CURDATE();</check-valid-connection-sql>
<!-- sql to call on an existing pooled connection when it is obtained from pool
<check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
-->

<!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->

<!-- Don't set this any higher than max_connections on your
MySQL server, usually this should be a 10 or a few 10's
of connections, not hundreds or thousands -->
<min-pool-size>1</min-pool-size>
<max-pool-size>10</max-pool-size>

<!-- Don't allow connections to hang out idle too long,
never longer than what wait_timeout is set to on the
server...A few minutes is usually okay here,
and how much spikey load it will see -->

<idle-timeout-minutes>5</idle-timeout-minutes>

<!-- If you're using Connector/J 3.1.8 or newer, you can use
our implementation of these to increase the robustness
of the connection pool. -->

<exception-sorter-class-name>com.mysql.jdbc.integration.jboss.ExtendedMysqlExceptionSorter</exception-sorter-class-name>
<valid-connection-checker-class-name>com.mysql.jdbc.integration.jboss.MysqlValidConnectionChecker</valid-connection-checker-class-name>

<type-mapping>mySQL</type-mapping>
</local-tx-datasource>
</datasources>


For creating the database schema see the quartz site.

After this you can use quartz in your MBean like this :

Regards,
Louis

• ###### 7. Re: Call EJB on a Schedule?

I had exactly the same question was told that this is basic J2EE, which not even requires a special configuration.

There is a good page explaining the Timer:
http://www.onjava.com/pub/a/onjava/2004/10/13/j2ee-timers.html

Regards

• ###### 8. Re: Call EJB on a Schedule?

Hi there,

I have a similar problem. I want to deploy a "service" or something which is automatically working after deployment in a timed fashion. An EJB isn't created before first request for it AFAIK. So I can't use the EJB to ask for a timer to execute an action. Or am I wrong? How should I do it?

Ciao!
SF

• ###### 9. Re: Call EJB on a Schedule?

Use the ServletContextListener to create the first request.