o_oand0_0

软件开发
随笔 - 4, 文章 - 2, 评论 - 5, 引用 - 0
数据加载中……

2011年10月21日

基于单例模式的多键值序列号生成器实现(带缓存)

     摘要: 基本思路:
* 数据库表通过Hibernate映射为实体类,主键通过序列号生成器(KeyGenerator)生成;
* 序列号生成器以Singleton方式工作,并维护所有需要维护的实体的序列号(KeyBean);
* 列号生成器缓存一定的键值,避免每次取序列号都从数据库获取;
* 数据库建立实体和当前键值对应表,每从列号生成器获取一个序列时,同步到该键值对应表,避免宕机后不一致  阅读全文

posted @ 2011-10-21 17:15 o_oand0_0 阅读(2195) | 评论 (3)编辑 收藏

基于spring+quartz开发定时器

1、准备Jar包
       在Spring所有包齐全的前提下还要导入一个定时器工具包:quartz-1.6.2.jar <http://www.opensymphony.com/quartz/download.action>
2、开发定时器类,实例代码如下:
 1public class TriggerUtil {
 2
 3    private TriggerUtil(){
 4        
 5    }

 6    
 7    public void expDataBase(){
 8        System.out.println("trigger actived..");
 9    }

10    
11}
3、配置定时任务
     为了清晰代码结构,单独建立一个配置定时任务的配置文件context-trigger.xml,并在applicationContext.xml进行import:
<import resource="context-trigger.xml"/>
    
     context-trigger.xml内容如下:
 1<?xml version="1.0" encoding="UTF-8"?>
 2<beans
 3    xmlns="http://www.springframework.org/schema/beans"
 4    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
 6    <!-- 定时器配置 -->
 7<!-- 配置定时器类 -->
 8  <bean id="triggerUtil" class="com.pro.base.util.TriggerUtil" >
 9  </bean>
10  <!-- 指定任务(方法) -->
11  <bean id="expDataBaseJob"
12      class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
13      <property name="targetObject">
14          <ref local="triggerUtil" />
15      </property>
16      <property name="targetMethod">
17          <value>expDataBase</value>
18      </property>
19  </bean>
20  <!-- 设定计划执行时间 -->
21  <bean id="expDataBaseTrigger"
22      class="org.springframework.scheduling.quartz.CronTriggerBean">
23      <property name="jobDetail">
24          <ref local="expDataBaseJob" />
25      </property>
26      <property name="cronExpression">
27          <value>00 33 21 * * ?</value>
28      </property>
29      </bean>
30      <!-- 任务执行器配置 -->
31      <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
32        <property name="triggers">
33            <list>
34                <ref local="expDataBaseTrigger" />
35            </list>
36        </property>
37    </bean>
38</beans>

附:定时时间配置说明
Expression    Meaning                                                                                                                                                                                      
0 0 12 * * ?     每天中午12点触发【Fire at 12pm (noon) every day 】                                                                                                                                           
0 15 10 ? * *     每天上午10:15触发【Fire at 10:15am every day 】                                                                                                                                              
0 15 10 * * ?     每天上午10:15触发【Fire at 10:15am every day 】                                                                                                                                              
0 15 10 * * ? *     每天上午10:15触发【Fire at 10:15am every day 】                                                                                                                                              
0 15 10 * * ? 2005     在2005这一年中每天上午10:15触发【Fire at 10:15am every day during the year 2005 】                                                                                                           
0 * 14 * * ?     每天下午14:00到15:00之间,每1分钟触发一次【Fire every minute starting at 2pm and ending at 2:59pm, every day 】                                                                             
0 0/5 14 * * ?     每天下午14:00到14:55之间,每5分钟触发一次【Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day 】                                                                          
0 0/5 14,18 * * ?     每天的14:00~14:55和18:00~18:55之间,每5分钟触发一次【Fire every 5 minutes starting at 2pm and ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending at 6:55pm, every day 】 
0 0-5 14 * * ?     每天的14:00~14:05之间,每1分钟触发一次【Fire every minute starting at 2pm and ending at 2:05pm, every day 】                                                                                
0 10,44 14 ? 3 WED     3月的每周三的14:10和14:44触发【Fire at 2:10pm and at 2:44pm every Wednesday in the month of March. 】                                                                                       
0 15 10 ? * MON-FRI     每周周一到周五的10:15触发【Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday 】                                                                                         
0 15 10 15 * ?    每月15日的10:15触发【 Fire at 10:15am on the 15th day of every month 】                                                                                                                     
0 15 10 L * ?     每月最后一天的10:15触发【Fire at 10:15am on the last day of every month 】                                                                                                                  
0 15 10 ? * 6L     每月的最后一个周五的10:15触发【Fire at 10:15am on the last Friday of every month 】                                                                                                         
0 15 10 ? * 6L 2002-2005     在2002到2005之间,每月的最后一个周五的10:15触发【Fire at 10:15am on every last friday of every month during the years 2002, 2003, 2004 and 2005 】                                          
0 15 10 ? * 6#3     每月的第三个星期五的10:15触发【Fire at 10:15am on the third Friday of every month 】                                                                                                        

posted @ 2011-10-21 16:20 o_oand0_0 阅读(1354) | 评论 (0)编辑 收藏

Java下载指定url的资源到指定路径

import java.net.*;
import java.io.*;

 1public void downLoadFile(String srcUri, String destPath) {
 2        FileOutputStream fos = null;
 3        BufferedInputStream bis = null;
 4        HttpURLConnection httpUrl = null;
 5        URL url = null;
 6        int bsize = 1024;
 7
 8        byte[] buf = new byte[bsize];
 9        int size = 0;
10        try {
11            url = new URL(srcUri);
12            httpUrl = (HttpURLConnection) url.openConnection();
13            httpUrl.connect();
14            bis = new BufferedInputStream(httpUrl.getInputStream());
15
16            fos = new FileOutputStream(destPath);
17            while ((size = bis.read(buf)) != -1{
18                fos.write(buf, 0, size);
19            }

20            fos.flush();
21        }
 catch (IOException e) {
22        }
 catch (ClassCastException e) {
23        }
 finally {
24            try {
25                fos.close();
26                bis.close();
27                httpUrl.disconnect();
28            }
 catch (IOException e) {
29            }
 catch (NullPointerException e) {
30            }

31        }

32    }

测试:

downLoadFile("http://www.baidu.com/img/baidu_sylogo1.gif","c:\\baidu_sylogo1.gif");


 

posted @ 2011-10-21 16:17 o_oand0_0 阅读(430) | 评论 (0)编辑 收藏