zoukankan      html  css  js  c++  java
  • Spring中quartz任务调度示例(获取腾讯天气预报)

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
       
       
        <!-- 配置quartz调度,指定加载哪些触发器-->
        <bean id="scheduler1"
            class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="triggers">
                <list>
                    <ref bean="GetweatherTrigger" />
                   
                </list>
            </property>
        </bean>
       
       
        <!-- 配置获取天气预报时间触发bean -->
        <bean id="GetweatherTrigger"
            class="org.springframework.scheduling.quartz.CronTriggerBean">
            <property name="jobDetail">
                <ref bean="Getweather" />
            </property>
            <!-- 配置 cronExpression表达式  也就是配置执行时间-->
            <property name="cronExpression" value="0 0/1 0-23 * * ?" />
        </bean>
       
       
        <!-- 配置执行任务bean 以及方法-->
        <bean id="Getweather" lazy-init="false" autowire="no"
            class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
            <property name="targetObject" ref="GetweatherOnTime" />
            <property name="targetMethod" value="start" />
        </bean>
    <!-- 配置需要定时执行的spring bean-->
        <bean id="GetweatherOnTime" class="com.misrs.syjpg.dss.was.dao.util.Httpweather">
     
        </bean>
       
     
        <!-- 为天气预报注入hibernatedao -->
     
        <bean id="httpweather" class="com.misrs.syjpg.dss.was.dao.util.Httpweather">
            <property name="hibernateDao">
                <ref bean="hibernateDao" />
            </property>
        </bean>
       
    </beans>
    执行获取天气预报的java代码

    /**
    *  @creator       季枫
    *   @create-time   2009-3-7   上午11:15:50  
    */
    package com.misrs.syjpg.dss.was.dao.util;
     
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.UnsupportedEncodingException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import com.misrs.syjpg.dss.was.dao.HibernateDao;
    import com.misrs.syjpg.dss.was.dao.po.SjyWeatherForecast;
    import com.misrs.syjpg.dss.was.dao.po.SjyWeatherInfo;
     
    public class Httpweather{
        /**
         *  @creator       季枫
         *   @create-time   2009-3-7   上午11:15:50
         *  @note          HTTP获取腾讯天气预报
         */
     
        private static HibernateDao hibernateDao;
        @SuppressWarnings("unchecked")
        public static void main(String[] args) {
           
            String url="http://weather.news.qq.com/inc/07_dc166.htm";
            /*String HTML=gethtml(url);
            String patternString="<td height=\"60\" align=\"center\">(.*?)</td>";
            List a=Regular(patternString,HTML);
            List b;
            for (int i=0;i<a.size();i++){
                System.out.println("------------------------------------");
                System.out.println(a.get(i).toString());
                b=Regular("<img width=\"44\" height=\"44\" src=\"(.*?)\">",a.get(i).toString());
                for (int j=0;j<b.size();j++){
                    System.out.println(b.get(j).toString());
                }
            }*/
           
            weatherinfo(url,"成都","166");
            //String[][][] cityinfo=ArrayCatalog1();
           
            //System.out.println(cityinfo[2][2][0]);
        }
       
        public void start(){
            System.out.println("spring Quartz run:HTTP获取腾讯天气预报!");
            String url="http://weather.news.qq.com/inc/07_dc";
            String[][][] cityinfo=ArrayCatalog1();
            System.out.println(cityinfo.length);
            for (int i=0;i<cityinfo.length;i++){
                weatherinfo(url+cityinfo[i][0][0]+".htm",cityinfo[i][2][0],cityinfo[i][0][0]);
               
            }
        }
       
       
        public static int weatherinfo(String url,String city,String code){
           
            /*ApplicationContext c = new ClassPathXmlApplicationContext(
                       new String[] {"applicationContext.xml", "applicationContext-was.xml"});
            HibernateDao hibernateDao=(HibernateDao) c.getBean("hibernateDao");*/
           
           
            String [] patternString=new String[13];
            String resulthtml;            //页面html代码
            List templist;
            String localfilepath="D:\\Program Files\\java\\apache-tomcat-6.0.18\\webapps\\ROOT\\web\\src\\img\\";        //保存本地路径
            String tomcatpath="/web/src/img/";
            String urlfilename;            //文件名
            String [] temp;
            resulthtml=gethtml(url);    //获取页面源码
           
            //当天情况
            SjyWeatherInfo sjyWeatherInfo=new SjyWeatherInfo();        //PO对象
            sjyWeatherInfo.setCity(city);
            sjyWeatherInfo.setCitycode(code);
            //当天图片
            temp=new String[3];
            templist=new ArrayList();
            patternString[0]="height=\"64\" src=\"(.*?)\"></td>";        //图片
            templist=Regular(patternString[0],resulthtml);
                //处理图片路径问题
                patternString[0]=(String) templist.get(0);
                templist=Regular("/images/(.*?).gif",patternString[0]);        //正则处理图片文件名  /big/abc.gif
                urlfilename=(String) templist.get(0);
                urlfilename=urlfilename.replace("/", "\\")+".gif";        //文件名
                if (!downloadImage(patternString[0],urlfilename,localfilepath))        //开始下载图片保存文件
                   {
                       System.out.println("下载图片出错!");
                       return 0;
                   }
                sjyWeatherInfo.setImgone((tomcatpath+urlfilename).replaceAll("[\\\\]","/"));        //相对与tomcat的路径保存到数据库
               
            //当天天气状况
            patternString[1]="#EDF2F8\" class=\"blues\">(.*?)</td>";
            templist=new ArrayList();
            templist=Regular(patternString[1],resulthtml);
                //处理格式问题
                patternString[1]=templist.get(0).toString();
                patternString[1]=patternString[1].replaceAll("\\s|\\n|(<.*?>)", "");
                sjyWeatherInfo.setWeather(patternString[1]);                //数据放入PO
            //当天描述
            patternString[2]="explain blues\">(.*?)<!--";
            templist=new ArrayList();
            templist=Regular(patternString[2],resulthtml);
                System.out.println("天气描述:"+templist.get(0).toString().replaceAll("<.*?>|\\p{Space}", ""));
                patternString[2]=templist.get(0).toString().replaceAll("<.*?>|\\p{Space}", "");
                patternString[2]=patternString[2].replaceAll("·", "&nbsp;");
                sjyWeatherInfo.setDescriptor(patternString[2]);                //放入PO
     
            SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
            String datestr=dateformat.format(new Date());
            try {
                sjyWeatherInfo.setTdate(dateformat.parse(datestr));        //初始化PO的日期数据
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            List wearlist=new ArrayList();   
           
            //准备检查当天是否有已经入库情况
            String uuid=UUIDGenerator.getUUID();
            sjyWeatherInfo.setId(uuid);                //先设置一个UUID
            wearlist = hibernateDao.sqlfind("select id from SjyWeatherInfo where tdate=TO_DATE('"+datestr+"','yyyy-MM-dd') and citycode='"+code+"'");
            if (wearlist.size()<=0)
                {    hibernateDao.save(sjyWeatherInfo);                //保存PO中的数据
                    uuid=sjyWeatherInfo.getId();
                   
                }
            else{
                    System.out.println("INFO表的ID:"+wearlist.get(0).toString());
                    uuid=wearlist.get(0).toString();
                    sjyWeatherInfo.setId((String) uuid);
                    hibernateDao.<span style
    还有些代码丢失了

  • 相关阅读:
    Unlicensed ARC session – terminating!
    ArcGIS读取dem格式数据
    OCIEnvCreate 失败,返回代码为 -1的解决方法
    PowerDesigner设计的数据库 ORA-0092
    Oracle空间查询 ORA-28595
    PowerDesigner添加表注释
    C# 动态解析表达式
    远程桌面不能交互复制粘贴
    ArcGIS10.4 Runtime Error R6034
    ArcGIS Add-in ValidateAddInXMLTask”任务意外失败
  • 原文地址:https://www.cnblogs.com/jifeng/p/1707552.html
Copyright © 2011-2022 走看看