How to schedule a job on server in Java web application - java

I am new to J2EE and i am working on couple of tasks. one of them is :
I have a web application that works like a reporting toolbox hosted by Apache tomcat 7, I need a heavy weight job to be scheduled to run every hour or other intervals, I googled and find Apache Sling that is kind of separate application server for content-centeric applications. I want to know if there is other solution could be done Apache tomcat or not ?
also its important that solution would be standard and reliable.

There's the ScheduledExecutorService which is part of the standard java api. See the new*Schedule* factory methods in Executors.
For a more heavyweight / configurable option there's Quartz. One of Quartz' nice features is it's support for cron expressions

You can also use Spring Batch. Here's a link that can help you understand this framework.
http://projects.spring.io/spring-batch/faq.html

In case none of the packages work for you one option would be to implement a ServletContextListener. It is an object that is launched when your site goes online. The only problem is that you have to manage all the scheduling.

Related

Is it possible to use Apache Airflow for choreography tasks without the scheduler?

I'm trying to develop a choreography architecture using the Apache Airflow Framework, but I don't want the DAGs to run in a scheduled way.
I read that the Apache Airflow framework can be used for service choreography. But I don't need any scheduling, I just need the framework to get a choreography between the nodes of a workflow.
Is it possible to use Apache Airflow without the scheduler? Is there any other framework (Java or Python) that you recommend me for this?
Thank you! :)
You can certainly create unscheduled DAGs
schedule_interval=None
You'll have to have the scheduler running to execute the task though, which you do like this:
airflow trigger_dag my-dag
As for alternatives, I suppose it all depends on what you are trying to accomplish. Most of the big choreography solutions have some scheduling built into the run-time. Apache Beam doesn't, but that's more specifically for data transformation and stream processing.
The closest thing might be luigi: https://github.com/spotify/luigi. It's a python project that allows you to choreograph tasks like AirFlow. It seems not to have a scheduler. It also seems to have a much simpler UI.
Uber Cadence Workflow is perfect for service orchestration scenarios. This answer provides more info.

Best way to schedule certain task in Java

I'm working on Java web application which requires running certain task (mysql query) once a day. What is the best way to do such thing in Java?
I saw something like Quartz but I'm not sure if it is ok for job like this. With quick review it looks like quartz requires initialization. Can it be done with web application? What about downtimes?
quartz is a great solution (the documentation tells you how to start it in a servlet container). it's also a very popular solution so there is plenty of documentation online.
if you are running within a full j2ee container, there is a built in timer service.

Job Scheduling in Java

I want to run a Java program on a specific date.
I am developing a J2EE application that allows you to schedule Selenium test launch (JUnit) on a specified date..
Are there any solutions to do this? can you point me to technology that can help me to do this?
any help is appreciated:)
thanks for your help
You provided very little information. You can schedule launch in scheduler of your operating system (like cron in Linux), or you can run a task from within your Java process, if the process is constantly running. For this see Quartz Scheduler.
Not knowing enough details, I would recommend using Quartz. You can see an example of using it here.
You could use crond or Windows Task Manager.
If you have a Java process running from now to the time it needs to start, look at Quartz.
If you need to have a Java process started from nothing, you must ask your operating system to invoke it for you. For Linux check the "at" command.
Cron on Unix, and Cron for NT on WindowsNT platforms (XP-Windows 7, Windows Server 4.0+).
Why reinvent the wheel?
If you want to create and package modular java server-side tasks (that you can then schedule in any particular java scheduler of your choice) check out the open source project called soafaces. Let's you create modular java Tasklets and also give them web based GUI customizer (customizer part is optional and based on google gwt).
Scheduling can be implemented in many ways, it is also bit IO intensive, so if needed u might want to use non-java solutions
However you want to have java solutions may be below links should help you
Spring Way : https://spring.io/guides/gs/scheduling-tasks/ and https://dzone.com/articles/schedulers-in-java-and-spring
Non Spring solution: https://github.com/knowm/Sundial

A Java HTTP Server

I want to implement a Java HTTP server locally, I mean the server computer would be in my control. I'm expecting not more than 20 clients to send requests to it.
I was wondering how to go about it:
Should I use a J2EE servlet container, like Apache Tomcat?
Could I use J2SE classes and just build it using them?
Are there any existing alternatives?
What does your experience suggest?
There's a simple HTTP server embedded in the Sun 1.6 JRE. It's not JavaEE or servlet-compliant, it's very lightweight, but it might be good enough for your requirements. No need to download any 3rd party stuff if this is all you need.
The javadocs, rather bizarrely, are out on their own, here.
Embed Jetty in your application. Aside from performing quite well, it is delightfully easy to use and configure
You've got many options, not the least of which are Jetty, Grizzly, and TTiny.
I would strongly urge against writing your own web server, unless you've got time to kill and want to spend it writing things that are already available to you for free.
Seriously, reuse an existing solution. Why the hell are you even thinking rolling your own?
Now, 1. I don't understand your question as being about embedding a container. 2. You mentioned long polling several time. So I'd suggest to use GlassFish v3 / Grizzly (because there are many samples, e.g. have a look at the Dead Simple Comet Example on Glassfish v3 / Grizzly).
If you don't want to rely on the way a container implemented Comet support, use atmosphere and any of the container mentioned on the web site:
Atmosphere is a POJO based framework using Inversion of Control (IoC) to bring push/Comet to the masses! Finally a framework which can run on any Java based Web Server, including Google App Engine, Tomcat, Jetty, GlassFish, Weblogic, Grizzly, JBossWeb and JBoss, Resin, etc. without having to wait for Servlet 3.0 Async support or without the needs to learn how Comet support has been differently implemented by all those Containers.
If this is not a concern, just stick with the suggested option (GlassFish v3 / Grizzly).
For a concrete and recent comparison between Comet server implementation, checkout this awesome Comet Maturity comparison grid view (source: Comet Gazing: Maturity). It might help you to make your final choice... or not :)
I guess the biggest question is: why do you want to build this?
If it is for the purpose of personal development, I'd stick to whatever standard libraries come with your JDK and build it on top of that.
If on the other hand you have a particular application that needs a dedicated HTTP server I would try to take one of the open source servlet containers, like Jetty or Tomcat and build on those.
Perhaps look at the list of 26 open source web servers at http://java-source.net/open-source/web-servers.
http://java.sun.com/developer/technicalArticles/Networking/Webserver/WebServercode.html is actual code in a single file implementing a multi threaded webserver. For your requirements, such as they are, this should suffice.
http://java.sun.com/developer/technicalArticles/Networking/Webserver/ is an analysis of the code.
If you will write your own HttpServer you will have to implement all the methods of the HTTP protocol. Tomcat can be easily used locally.
Is it for practice, fun, to implement special requirements or why don't you just embed an existing solution?
Do you really want to build a HTTP server that deals with the protocol directly, or do you just want to write web apps? If all you care about is writing the web apps, then just use Tomcat, or Jetty, or Glassfish, or another server -- it will save you a ton of work.
If you really are interested in writing your own server from scratch, then the best way would be to just use Java SE, and not use any existing server technology.
Ad your 3) option: Try JBoss Netty.
http://fisheye.jboss.org/browse/Netty/trunk/src/main/java/org/jboss/netty/example/http/websocket

How do you launch multiple threads from within Java EE?

I need to scale calls into Tomcat and it's been suggested to launch threads internally. Has anyone needed to do this and, if so, what solutions did they come up with?
Creating your own threads inside an application server is generally discouraged because the server should manage threads for better scalability. You can also run into problems if the container makes assumptions about what's available in a thread context, such as security information (e.g., authenticated Subject). That typically happens if you spawn a thread and then use a server resource from that thread which is unknown to the container.
Check to see if there is a way to get container managed threads from Tomcat. WebLogic and WebSphere support the commonj.WorkManager, which allows you to schedule work on container managed threads. Spring can also use commonj, but I'm not sure if that support is available on Tomcat.
You shouldn't really launch threads from within your webapp unless you have a very specific need to do so. Without more details on your problem it is hard to tell if this is the right approach to solve your problem.
You might want to take a look at Quartz, which "is a full-featured, open source job scheduling system that can be integrated with, or used along side virtually any J2EE or J2SE application".
Your question is a bit vague. Tomcat itself already uses a thread pool to service HTTP requests. You can increase the number of threads through Tomcat configuration - look to the Tomcat wiki for info on this.
If you mean that in your code you want to launch threads, then I advise perusing the java.util.concurrent API introduced in Java 5. Also read "Java Concurrency in Practice", which is the text on this subject.
What is the problem you are trying to solve with threads?
If have long running tasks you should use JMS + a full Java EE container.
If you trying to handle excess load you could consider two tomcat instances, however, if you are using http sessions you will need to investigate session replication.
If you are forced to use Tomcat consider using the Executors framework in java.util.concurrency.
as others asked, you should give more details as to what you're trying to accomplish.
Otherwise, tomcat uses thread pools. increase the number of threads in the pool. Use a newer version of tomcat -- 6.x. Use Java 6.0_10. If needed, tune the application using a profiler and fiddle with the JVM settings, if required.
The J2EE abstraction for managed multithreading is JCA. In particular, take look at the WorkManager and Work classes. See also this arcicle. Spring also provides JCA-backed work manager abstraction.

Categories

Resources