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);
}
}
}
 

ThreadPoolTaskExecutor异步的处理报警发送邮件短信比较耗时的东东的更多相关文章

  1. centos7之zabbix邮件报警(短信报警)

    前言 前面我们介绍了zabbix的基本linux和window及SNMP流量的简单监控,我们知道作为运维人员,需要7x24小时待命,但是我们不可能时时刻刻都坐在电脑旁边查看监控上的各个主机状态,所以我 ...

  2. python 发送邮件短信封装

    发送邮件 需要开启163的smtp服务 import smtplib from email.mime.text import MIMEText class MailSender(): def __in ...

  3. zabbix学习笔记:zabbix监控之短信报警

    zabbix学习笔记:zabbix监控之短信报警 zabbix的报警方式有多种,除了常见的邮件报警外,特殊情况下还需要设置短信报警和微信报警等额外方式.本篇文章向大家介绍短信报警. 短信报警设置 短信 ...

  4. 1、背景介绍及移动云MAS平台 --短信平台

    目的: 刚开发完成一套短信平台以及一个Web端短信发送系统,短信平台耗时两个周.短信发送系统耗时两个多月,开发使用的技术没什么高科技含量,在此主要是记录下很多情况的处理方案,希望能让大家提出改善方案和 ...

  5. mysubmail 短信报警

    https://www.mysubmail.com/chs/documents/developer/YPWD84   文本文档  官网:www.mysubmail.com 操作流程:快速接入短信 AP ...

  6. zabbix3配置短信报警

    需求:在zabbix服务器配置短信报警,当服务出现故障达到预警级别是通过发送短信的形式告诉运维人员,以便及时处理. 一.zabbix服务器端配置短信脚本 我的短信脚本放置位置为 /etc/zabbix ...

  7. a标签的妙用-拨打电话、发送短信、发送邮件

    前端时间在做手机WAP网站时,遇到需要点击页面上显示的电话号能直接拨号的需求,查找资料发现可以使用html的a标签完美实现该需求!记录下来以备后用...... 目前主流手机浏览器对H5的支持已经很不错 ...

  8. CactiEZ 中文版V10.1安装使用以及139邮箱短信报警设置

    说明:CactiEZ中文版V10.1是基于CentOS 6.0系统,整合Cacti等相关软件,重新编译而成的一个操作系统!   说明:CactiEZ中文版V10.1是基于CentOS 6.0系统,整合 ...

  9. server宕机监控、检測、报警程序(139绑定手机短信报警)monitor_down.sh

    宕机监控报警程序 一.   需求来源 宕机对运维人员来说,最痛苦了.怎样检測一台server是否还在正常执行,假设该server宕机,怎样在第一时间监測到并通知一线运维人员进行维护,最大化降低损失. ...

随机推荐

  1. SQLServer异常捕获

    在SQLserver数据库中,如果有很多存储过程的时候,我们会使用动态SQL进行存储过程调用存储过程,这时候,很可能在某个环节就出错了,但是出错了我们很难去跟踪到出错的存储过程,此时我们就可以使用异常 ...

  2. 在VS2010 SP1基础上安装mvc3

    安装VS2010 SP1后,再安装mvc3会报错,估计原因是此安装包会安装VS的补丁,而sp1的补丁版本高过此安装包的. AspNetMVC3ToolsUpdateSetup.exe 解决办法: 运行 ...

  3. golang的goroutine与channel

    Golang的goroutine是非抢占式的, 令人相当蛋疼! 有痛不能呻吟...只能配合channel在各goroutine之间传递信号来实现抢占式, 而这形成了golang最灵活与最具性能的核心. ...

  4. [转]ubuntu(12.04)下, 命令 ,内核 源代码的获取

    [转]ubuntu(12.04)下, 命令 ,内核 源代码的获取 http://blog.chinaunix.net/uid-18905703-id-3446099.html 1.命令:例如:要查看l ...

  5. Android编程: 环境搭建、基本知识

    学习的内容两个方面:环境搭建.基本知识 ====环境搭建==== 1.下载 android studio(http://developer.android.com/sdk/index.html) 2. ...

  6. Object:

    所有类的直接或者间接父类,Java认为所有的对象都具备一些基本的共性内容,这些内容可以不断的向上抽取,最终就抽取到了一个最顶层的类中的,该类中定义的就是所有对象都具备的功能. 具体方法: 1,bool ...

  7. My mac cannot run 类相关的操作 , which is lower than 类相关的操作。

     首先你选择的项目是mac项目,    其次,你MAC的系统版本小于你当前项目部署环境的最低支持版本    要么升级你的MAC系统,要么再project—>target设置developerme ...

  8. C语言中结构体的初始化

    直接上例子: struct point { int x; int y; int z; } //常规写法 struct point pt1 = {100, 300, 200}; //初始化个数少于实际个 ...

  9. 说明&总目录

    1. 说明 1.1 这是一个乱七八糟的博客,包含遇到的各类问题,甚至会有奇♂怪的东西~ 1.2 作者目前本科生,懒虫一只,喜欢吃喝玩乐看动漫,更喜欢睡觉 1.3 文章难免有错,欢迎指出 1.4 语死早 ...

  10. copy-mutableCopy

    copy和mutableCopy语法的目的:改变副本的时候,不会影响到源对象:调用Copy产生的对象是不可变的,调用mutableCopy产生的对象是可变的,与调用对象是否可变无关. Copy 需要先 ...