---
title: "定时器第二集"
author: "vortexchoo"
date: "2013-07-17T17:19:57Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2013-07-17T17:19:57Z"
id: 1902910
url: "https://vortexchoo.iteye.com/blog/1902910"
---

前天的时候写了定时器监控网站的文章，当时理解的是找台机器放上去把main运行了就OK了，谁知领导说你做一个能挂服务器上的，到时候做个webservice接口，调用短信平台，如果网站无法登陆就直接发短信给用户。

于是就有了下面的东西：

先说jar包：spring.jar,quartz-all-1.8.5.jar,log4j-1.2.15.jar,axis.jar

先导包， 然后建立package创建类，我这里创建的类名为：QuartzJob，类中创建一个名为work的方法

之后再WEB-INF下简历一个xml文件，applicationContext\_quartz.xml

具体内容为：

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;beans  
xmlns="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans)"  
xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)"  
xmlns:jee="[http://www.springframework.org/schema/jee](http://www.springframework.org/schema/jee)"  
xsi:schemaLocation="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans) [http://www.springframework.org/schema/beans/spring-beans-2.0.xsd](http://www.springframework.org/schema/beans/spring-beans-2.0.xsd)  
[http://www.springframework.org/schema/jee](http://www.springframework.org/schema/jee) [http://www.springframework.org/schema/jee/spring-jee-2.0.xsd](http://www.springframework.org/schema/jee/spring-jee-2.0.xsd)"&gt;

        &lt;!-- 要调用的工作类 --&gt;  
        &lt;bean id="quartzJob" class="你的类的路径"&gt;&lt;/bean&gt;  
        &lt;!-- 定义调用对象和调用对象的方法 --&gt;  
         &lt;bean id="jobTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"&gt;  
            &lt;!-- 调用的类 --&gt;  
            &lt;property name="targetObject"&gt;  
                &lt;ref bean="quartzJob"/&gt;  
            &lt;/property&gt;  
            &lt;!-- 调用类中的方法 --&gt;  
            &lt;property name="targetMethod"&gt;  
                &lt;value&gt;此定时器执行的类中的方法名&lt;/value&gt;  
            &lt;/property&gt;  
        &lt;/bean&gt;

        &lt;!-- 定义触发时间 --&gt;  
        &lt;bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerBean"&gt;  
            &lt;property name="jobDetail"&gt;  
                &lt;ref bean="jobTask"/&gt;  
            &lt;/property&gt;  
            &lt;!-- cron表达式 --&gt;  
            &lt;property name="cronExpression"&gt;  
                &lt;!-- 每隔10秒执行一次--&gt;  
                &lt;value&gt;0/10 * * * * ?&lt;/value&gt;  
            &lt;/property&gt;  
        &lt;/bean&gt;  
        &lt;!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 --&gt;  
        &lt;bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"&gt;  
            &lt;property name="triggers"&gt;  
                &lt;list&gt;  
                    &lt;ref bean="doTime"/&gt;  
                &lt;/list&gt;  
            &lt;/property&gt;  
        &lt;/bean&gt;  
&lt;/beans&gt;

最后需要做的就是配置web.xml文件

具体内容：

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;web-app version="2.4"  
 xmlns="[http://java.sun.com/xml/ns/j2ee](http://java.sun.com/xml/ns/j2ee)"  
 xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)"  
 xsi:schemaLocation="[http://java.sun.com/xml/ns/j2ee](http://java.sun.com/xml/ns/j2ee)  
 [http://java.sun.com/xml/ns/j2ee/web-app\_2\_4.xsd](http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd)"&gt;  
  &lt;welcome-file-list&gt;  
    &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;  
  &lt;/welcome-file-list&gt;  
  &lt;!-- 加载spring --&gt;  
&lt;context-param&gt;  
   &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;  
   &lt;param-value&gt;  
    /WEB-INF/applicationContext\*.xml  
   &lt;/param-value&gt;  
&lt;/context-param&gt;  
&lt;servlet&gt;  
   &lt;servlet-name&gt;context&lt;/servlet-name&gt;  
   &lt;servlet-class&gt;  
    org.springframework.web.context.ContextLoaderServlet  
   &lt;/servlet-class&gt;  
   &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;  
&lt;/servlet&gt;  
&lt;/web-app&gt;

-----至此一个定时器就做好了，具体的方法体我就不写了。

下来来说webserice远程调用

 package com.foresee.ws;

import java.rmi.RemoteException;

import javax.xml.namespace.QName;  
import javax.xml.rpc.ServiceException;

import org.apache.axis.client.Call;  
import org.apache.axis.client.Service;

public class WsUtil {  
   
 private WsUtil(){}  
   
  private static WsUtil instance = null;  
   
 public static WsUtil getInstance(){  
  if(instance==null){  
   instance = new WsUtil();  
  }  
  return instance;  
 }  
   
 static String URL = "[http://具体暴露接口的URL?wsdl](http://%E5%85%B7%E4%BD%93%E6%9A%B4%E9%9C%B2%E6%8E%A5%E5%8F%A3%E7%9A%84url/?wsdl)";//这里涉及公司隐私就不写了  
 public static String invokeWs(String sid, String xml){  
  String returnXml = null;  
  Service service = new Service();  
  Call call = null;  
    
  try {  
   call = (Call) service.createCall();  
   call.setTargetEndpointAddress(URL);  
   call.setTimeout(new Integer("400"));  
   call.setOperationName(new QName(URL,"invoke"));  
   returnXml = (String) call.invoke(new Object\[]{sid, xml});  
  } catch (ServiceException e) {  
   // TODO Auto-generated catch block  
   e.printStackTrace();  
  } catch (RemoteException e) {  
   // TODO Auto-generated catch block  
   e.printStackTrace();  
  }  
  return returnXml;  
 }  
}

上面把这个类加工成了单例模式，因为是挂在定时服务上的，所以没必要每次都去实例化。

返回的是一个XML文件，因为webservice是用xml文件来进行数据交互的。如果有必要可以将xml文件中的内容解析出来，google上现成的解析代码是有的。

至此搞定了。![酷](http://www.iteye.com/javascripts/tinymce/plugins/emotions/img/smiley-cool.gif "酷")