How to start in Web Application examples explain the background tasks

  Abstract: How to start in Web Application examples explain the background tasks 

  </ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> 

  We often in Web applications need to write its own services, the purpose of this paper is to provide you with a solution. 

  <table Width="674" border="0"> <tr> <td width="396"> 

  Principle 

  The principle of the programme is to write an interface to achieve the ServletContextListener category, there are two such methods: 

  Public void contextInitialized (ServletContextEvent sce) 

  It is activated when the call in the application; Another method is: 

  Public void contextDestroyed (ServletContextEvent sce) 

  The method is applied at the end of the call. 

  </ Td> <td width="268"> </ td> </ tr> </ table> 


  We have to start the application logic on the background contextInitialized method achieved; to release background applications work in the occupied contextDestroyed resources to deal with them.    But we started background task is often required, such as time, frequency, and so on, here I use an open-source components: quartz. 

  Steps 

  1, writing business called categories: 

  <table CellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6> 

  / / DumbJob.java import org.quartz .*; import java.util .*; public class DumbJob implements Job (public DumbJob () () public void execute (JobExecutionContext context) throws JobExecutionException (/ / write processing code here.    ))    </ Td> </ tr> </ table> 



  This is the main function of quartz in the scheduling of the prescribed rules call implement the necessary business logic. 

  2, writing class scheduling 

  <table CellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6> 

  / / TestShedule.java import org.quartz .*; import java.util .*; public class TestShedule = (static SchedulerFactory schedFact new org.quartz. Impl.StdSchedulerFactory (); static Scheduler sched; public static void run () throws Exception (= schedFact.getScheduler sched () / / Get Scheduling Manager JobDetail jobDetail = new JobDetail ( "myJob" sched.DEFAULT_GROUP, DumbJob.class); / / create work CronTrigger trigger = new CronTrigger ( "myTrigger", "test "," 0 / 10 * * * *? "); / / create Trigger sched.scheduleJob (jobDetail, trigger); / / added to the scheduling manager in sched.start ();// start scheduling management of public) static void stop () throws Exception (sched.shutdown ();))    </ Td> </ tr> </ table> 



  The purpose of this call is set up the rules, and here I use the "0 / 10 * * * *?" Said that every 10 seconds once. 

  3, prepared by the start of services: 

  <table CellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6> 

  / / ServiceLoader.java import javax.servlet. ServletContextEvent; import javax.servlet. ServletContextListener; public class ServiceLoader implements ServletContextListener (public void contextInitialized (ServletContextEvent sce) (try (TestShedule.run ();) catch (Exception ex) (System. out.println (ex.getMessage ());)) public void contextDestroyed (ServletContextEvent sce) (try (TestShedule.stop ();) catch (Exception ex) (System.out.println (ex.getMessage ());) ))    </ Td> </ tr> </ table> 



  Calling in contextInitialized TestShedule.run () start background task; contextDestroyed Calling TestShedule.stop () stop background tasks. 

  4, the deployment of such services launched 

  In web.xml file add the following line in the configuration: 

  <table CellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400 align=center borderColorLight=black border=1> <tr> <td class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6> 

  <? Xml version = "1.0" encoding = "UTF-8"> web-app PUBLIC "- / / Sun Microsystems, Inc / / DTD Web Application 2.3 / / EN" "http://java.sun.com / dtd/web-app_2_3.dtd ">    Root  ServiceLoader   Index.html    </ Td> </ tr> </ table> 



  5, web services can be launched. 

  Aggregate 

  In fact, the functions of the realization of this lot, I am here is the application interface and the ServletContextListener revenue api quartz, and hope that you can help the development.    Function TempSave (ElementID) (CommentsPersistDiv.setAttribute ( "CommentContent" document.getElementById (ElementID). Value); CommentsPersistDiv.save ( "CommentXMLStore");) function Restore (ElementID) (CommentsPersistDiv.load ( "CommentXMLStore"); document . getElementById (ElementID). CommentsPersistDiv.getAttribute value = ( "CommentContent");) </ td> </ tr> <tr> 

  ↑ Back 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags: ,

Releated Java Articles

Comments

Leave a Reply