前天的时候写了定时器监控网站的文章,当时理解的是找台机器放上去把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
具体内容为:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
<!-- 要调用的工作类 -->
<bean id="quartzJob" class="你的类的路径"></bean>
<!-- 定义调用对象和调用对象的方法 -->
<bean id="jobTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject">
<ref bean="quartzJob"/>
</property>
<!-- 调用类中的方法 -->
<property name="targetMethod">
<value>此定时器执行的类中的方法名</value>
</property>
</bean>
<!-- 定义触发时间 -->
<bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="jobTask"/>
</property>
<!-- cron表达式 -->
<property name="cronExpression">
<!-- 每隔10秒执行一次-->
<value>0/10 * * * * ?</value>
</property>
</bean>
<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="doTime"/>
</list>
</property>
</bean>
</beans>
最后需要做的就是配置web.xml文件
具体内容:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 加载spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext*.xml
</param-value>
</context-param>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
</web-app>
-----至此一个定时器就做好了,具体的方法体我就不写了。
下来来说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";//这里涉及公司隐私就不写了
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上现成的解析代码是有的。
至此搞定了。