ThreadPoolTaskExecutor异步的处理报警发送邮件短信比较耗时的东东



package
com.elong.ihotel.util; import org.springframework.beans.factory.DisposableBean; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /** * Created by zyp on 14-7-4. */ public class SpringWebContextHolder implements ApplicationContextAware, DisposableBean { private static ApplicationContext applicationContext = null; /** * 取得存储在静态变量中的ApplicationContext. */ public static ApplicationContext getApplicationContext() { return applicationContext; } /** * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型. */ @SuppressWarnings("unchecked") public static <T> T getBean(String name) { return (T) applicationContext.getBean(name); } /** * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型. */ public static <T> T getBean(Class<T> requiredType) { return applicationContext.getBean(requiredType); } /** * 清除SpringWebContextHolder中的ApplicationContext为Null. */ public static void clearHolder() { applicationContext = null; } /** * 实现ApplicationContextAware接口, 注入Context到静态变量中. */ @Override public void setApplicationContext(ApplicationContext applicationContext) { SpringWebContextHolder.applicationContext = applicationContext; } /** * 实现DisposableBean接口, 在Context关闭时清理静态变量. */ @Override public void destroy() throws Exception { SpringWebContextHolder.clearHolder(); } }

 

service.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:context="http://www.springframework.org/schema/context"

    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"

    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:oscache="http://www.springmodules.org/schema/oscache"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd

           http://www.springmodules.org/schema/oscache http://www.springmodules.org/schema/cache/springmodules-oscache.xsd">



    <context:property-placeholder

        location="classpath:conf/custom/env/config.properties"

        ignore-unresolvable="true" />

            

    <!-- spring 线程池配置 start -->

   

    

    <!-- 公用的线程池服务器,处理一些例如报警,写日志等的工作(线程池容量很小) -->

     <bean id="commonThreadPoolExecutor"

        class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">

        <property name="corePoolSize" value="${commonThreadPoolExecutor.corePoolSize}" />

        <property name="maxPoolSize" value="${commonThreadPoolExecutor.maxPoolSize}" />

        <property name="keepAliveSeconds" value="${commonThreadPoolExecutor.keepAliveSeconds}" />

        <property name="queueCapacity" value="${commonThreadPoolExecutor.queueCapacity}" />

    </bean>

    

</beans>

config.properties:

#coomon thread pool

commonThreadPoolExecutor.corePoolSize=1

commonThreadPoolExecutor.maxPoolSize=2

commonThreadPoolExecutor.keepAliveSeconds=20

commonThreadPoolExecutor.queueCapacity=100


利用线程池异步的发送报警:

 

package com.elong.ihotel.util;



import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;



import com.elong.common.util.log.model.AlarmEntity;

import com.elong.common.util.log.model.BaseAlarmEntityType;

import com.elong.ihotel.util.log.AlarmEntityType;



/**

 * Created by ggg on 2014/10/17.

 */

public class AlarmUtils {



    // 初始化线程池

    private static ThreadPoolTaskExecutor threadPoolExecutor = (ThreadPoolTaskExecutor) SpringWebContextHolder .getBean("commonThreadPoolExecutor"); /**

     * 发送短信

     * 

     * @title 发送报警短信方法,titile为短信内容,message为邮件内容

     * @param type

     * @param title

     * @param message

     * */

    public static void sendAlarm(BaseAlarmEntityType type, String title,

            String message) {



        AlarmEntity alarmEntity = new AlarmEntity(type);

        alarmEntity.setAlertTitle(title);

        alarmEntity.setAlertMessage(message);

        IHotelLoggerUtil.alarmInfo(alarmEntity);

    }



    /**

     * 发送报警短信短信

     * 

     * @title 发送报警短信方法,titile为短信内容,message为邮件内容

     * @param type

     * @param title

     * @param message

     * */

    public static void sendAlarm(AlarmEntityType type, String title,

            String message) {

        String serverName = "[机器名称]" + IPUtil.getServerName();

        AlarmEntity alarmEntity = new AlarmEntity(type.getType());

        alarmEntity.setAlertTitle(title + serverName);

        alarmEntity.setAlertMessage(message);

        IHotelLoggerUtil.alarmInfo(alarmEntity);

    }



    /**

     * 异步发送报警短信 sendAlarmAsync

     * 

     * @Title: sendAlarmAsync

     * @param type

     * @param title

     * @param message

     */

    public static void sendAlarmAsync(AlarmEntityType type, String title,

            String message) {

        try {

            String serverName = "[机器名称]" + IPUtil.getServerName();

            final AlarmEntity alarmEntity = new AlarmEntity(type.getType());

            alarmEntity.setAlertTitle(title + serverName);

            alarmEntity.setAlertMessage(message);



            if (threadPoolExecutor != null) { threadPoolExecutor.execute(new Runnable() { @Override public void run() { try { IHotelLoggerUtil.alarmInfo(alarmEntity); } catch (Exception ex) { IHotelLoggerUtil.error("异步发送报警执行异常", ex); } } }); }



        } catch (Exception ex) {

            IHotelLoggerUtil.error("异步报警执行异常", ex);

        }

    }

}

 

 

 

 

 

 

你可能感兴趣的:(TaskExecutor)