EJB 3.0 Timer Cluster Info - java

I was able to get some hands on EJB3.0 Timer Service.I was able to get the timeout working and I was able to invoke the timer using servlet Context listener.I have deployed a simple app which sends alerts at a specific interval.I am using WL 10.3.1(does not support EJB3.1,to use Scheduler).
I get alerts twice at the same time.(I have a cluster with 2 managed Servers).I looked at few examples of using a timer in WL cluster,for eg: http://shaoxiongyang.blogspot.com/2010/10/how-to-use-ejb-3-timer-in-weblogic-10.html .But I would like to avoid any configuration on the server.Is there any other way this can be controlled in a Cluster Env.I want to have one timer running at any time in a cluster Env.
Thanks...

Do the servlet context listeners unconditionally create the timer during contextInitialized? If so, that explains the problem since the servlet context listener will run in each JVM. You'll need to somehow check if the timer has already been created first. Either use getTimers or check/insert a row into your own database table.

Related

Springboot: need to be informed/notified when Web-Server is Up

I am writing a Springboot application embedding Tomcat as a Web Server.
At startup, some of my threads are ready (and so start doing their job) before Tomcat is. For many contraints I have, I want that these threads do nothing before Tomcat is ready
But I don't know how to decided to block/unblock my threads; to do so, I need to be informed of the Tomcat status. Is there:
a way to ask: isWebServerStarted()?
or a way to be notified asynchronously by a message saying: WEB_SERVER_IS_STARTED?
Thank you for help
FYI, I don't want to declare a kind of "private ReST endpoint" that my
application could try to reach in order to guess whether the webserver
is ready
Go with a ServletContextListener. Annotate your class with #WebListener and tell spring configuration about this listener by adding #ServletComponentScan annotation.

How to make sure that EJB timers run on single node at a time in clustered environment?

I have created 4 different EJB timers using EJB 3.0. A single session bean is used for each timer ( I can not use EJB 3.1).
I start all timers using a a class which is configured in weblogic-application.xml as follow:
<wls:listener>
<wls:listener-class>com.xyz.abc.TimerJob</wls:listener-class>
</wls:listener>
This class TimerJob accesses a stateless session bean TimerServiceBean which internally create all timer jobs at the server startup.
I have configured TimerServiceBean and all Timer sesssion beans to have single instance only as follow:
<wls:weblogic-enterprise-bean>
<wls:ejb-name>TimerServiceBean</wls:ejb-name>
<wls:stateless-session-descriptor>
<wls:pool>
<wls:max-beans-in-free-pool>1</wls:max-beans-in-free-pool>
</wls:pool>
</wls:stateless-session-descriptor>
This works fine in single server environment.
I deploy all this in clustered environment by following all steps mentioned in the artilce : http://shaoxiongyang.blogspot.in/2010/10/how-to-use-ejb-3-timer-in-weblogic-10.html
In clustered environment, the timers execute on each node
I want each timer to execute on single node after specific interval which I configured while creating timers.
Any suggestion in making them work on single node at a time. Thanks in advance for your valuable suggestions and replies.

how to start Quartz Scheduler automatically in Java

I'm using Quartz Scheduler 2.2.1 in a webapp which is built on Tomcat webserver. I use a servlet to start Quartz . However, if the system reboots, I also have to restart Quartz manually by sending request to that servlet. Therefore, the problem is how to start Quartz automatically !
One more thing that is I want to use Quartz to perform a task at 00:00:00 everyday, so what is the best design for the trigger in this case ?
Define a ServletContextListener and implement a contextInitialized() method that starts quartz. This listener gets triggered if the tomcat is restarted or your servlet is redeployed
You should use a custom ServletContextListener and configure it in your web.xml for automatically starting your cron job.
http://docs.oracle.com/cd/B14099_19/web.1012/b14017/filters.htm#i1000654
If not satisfied by the Oracle link, do a little Google and you will get plenty of examples
To start a cron job every day at 12:00 am the below cron pattern should work:
0 0 0 * * ?

How does the JDBCJobStore work .?

So I started to tinker around with JDBCJobStore in Quartz. Firstly, I could not find a single good resource on how to configure it from scratch. After looking for it for a while and singling out a good resource for beginners, I downloaded the sample application at Job scheduling with Quartz. I have a few doubts regarding it.
How does JDBCJobStore capture jobs.? I mean in order for the job to get stored in the database does the job have to run manually once.? Or will JDBCJobStore automatically detect the jobs and their details..?
How does JDBCJobStore schedule the jobs.? Does it hit the database at a fixed interval like a heartbeat to check if there are any scheduled jobs.? Or does it keep the triggers in the memory while the application is running.?
In order to run the jobs will I have to manually specify the details of the job like like name and group and fetch the trigger accordingly.? Is there any alternative to this.?
On each application restart how can I tell the scheduler to start automatically..? Can it be specified somehow.?
If you are using servlet/app server you can start it during startup:
http://quartz-scheduler.org/documentation/quartz-2.2.x/cookbook/ServletInitScheduler
If you are running standalone you have to initialize it manually i think.
You can read more about JobStores here:
http://quartz-scheduler.org/documentation/quartz-2.2.x/tutorials/tutorial-lesson-09
And about jobs and triggers:
http://quartz-scheduler.org/documentation/quartz-2.2.x/tutorials/tutorial-lesson-02
http://quartz-scheduler.org/documentation/quartz-2.2.x/tutorials/tutorial-lesson-03
http://quartz-scheduler.org/documentation/quartz-2.2.x/tutorials/tutorial-lesson-04
I guess that quartz checks jobs based on time interval to proper work in clusters and distributed systems.

Call method after JBoss deploy

I want to have some method run, only after my WAR has been deployed to JBoss.
The problem: Currently I am using #PostConstruct to load saved schedules from the DB. The problem is that I am creating instances of the Schedulers from this method, which in turn is starting the Quartz schedulers, which is stopping JBoss from completing the deploy.
If there are no schedules to load, my WAR deploys fine, but if there are schedules they are causing the deploy to fail, because JBoss is "waiting" for the schedules to actually complete.
Is there some way to delay the method call until after it is fully deployed?
Or alternatively, is it possible to make Async calls on the Server (from the Server code)?
Java EE specification heavily refrain any thread manipulation outside facility provided by the application server.
You shouldn't in any case use a container manged thread in an infinite loop; the container expect the thread to be returned. Thread creation can still be done without too much collateral damage (if you don't put several apps on the server as the container won't be able to manage the resources between all the applications) but the any container thread must be returned.
In the new Jboss 7 there is some Java EE scheduling facilities (#Scheduling and timer, with possible persistent timer). A quick search show some example how to run Quartz in JBoss 7: How to enable Quartz scheduling in Jboss AS 7.0?
In older JBoss more advanced integration exist (JCA integration is the only standard way to get finer thread management). Use google to find them.
Wouldn't the simple ServletContextListener solve the problem for you? Just implement whatever you need in contextInitialized method.
In JBoss 7, there is a management api.
Maybe you can use it to check if the server is started (with a singleton and a TimerService).
Sample code:
ModelControllerClient client = ModelControllerClient.Factory.create(InetAddress.getByName("localhost"), 9999);
ModelNode op = new ModelNode();
op.get(ClientConstants.OP).set("read-attribute");
op.get(ClientConstants.NAME).set("server-state");
ModelNode returnVal = client.execute(op);
if(StringUtils.equals(returnVal.get("result").toString(), "\"running\"")){
LOGGER.info("Server running, init start actions");
timer.cancel();
}else{
LOGGER.info("Server not running, wait");
}

Categories