Spring Integration - 自动轮询发送手机短信

Spring Integration 配置

<?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:int="http://www.springframework.org/schema/integration"
    xmlns:int-jpa="http://www.springframework.org/schema/integration/jpa"
    xmlns:int-jms="http://www.springframework.org/schema/integration/jms"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.2.xsd
            http://www.springframework.org/schema/integration/jms http://www.springframework.org/schema/integration/jms/spring-integration-jms.xsd
            http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
            http://www.springframework.org/schema/integration/jpa http://www.springframework.org/schema/integration/jpa/spring-integration-jpa-2.2.xsd
            ">    

    <int-jpa:inbound-channel-adapter
        auto-startup="true" entity-manager="em"
        send-timeout="60000" channel="process.channel"
        expect-single-result="true"
        jpa-query="SELECT sysdate FROM dual">
        <int:poller fixed-delay="60000" />
    </int-jpa:inbound-channel-adapter>

    <int:channel id="process.channel">
        <int:queue capacity="1"/>
    </int:channel>

    <int:chain input-channel="process.channel">    

        <int-jpa:retrieving-outbound-gateway entity-manager="em" jpa-query="SELECT sp FROM SmsMessage sp Where sp.tatus is null order by sp.requestOn,sp.id"/>    

        <int:splitter ref="process.processSplitter" method="split"/>

        <int:service-activator ref="process.smsSenderService"
            method="send" />

        <int:poller fixed-delay="5000" receive-timeout="-1"/>
    </int:chain>        

    <bean id="process.smsSenderService" class="com.yd.core.service.SmsSenderService" />        

    <bean id="process.processSplitter" class="com.yd.core.service.PaymentProcessSplitter" />
</beans>

Job Worker

import org.springframework.context.ApplicationContext;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.support.MessageBuilder;

public class JobWorker implements Runnable {

    private static final int DEFAULT_WAIT_TIME = 3000;

    @Override
    public void run() {
        while (true) {
            try {
                LoggerUtil.getJobLogger().info("JobWorker, Ready for take job run request.");

                JobRunnerRequest jobRequest = JobManagerService.getJobManager().takeRequest();
                while (jobRequest == null) {
                    LoggerUtil.getJobLogger().warn("JobWorker, jobRequest is null, will try to get the job requet again.");
                    Thread.sleep(DEFAULT_WAIT_TIME);
                    jobRequest = JobManagerService.getJobManager().takeRequest();
                }

                LoggerUtil.getJobLogger().info("JobWorker, Received a job run request.");

                MessageChannel channel = findChannel(jobRequest.getJobChannelId());
                if (channel != null) {
                    channel.send(MessageBuilder.withPayload(jobRequest.getJobMessagePayload()).build());
                    LoggerUtil.getJobLogger().info("JobWorker, Completed to sned message to job channel");
                }
            }
            catch (Exception ex) {
                LoggerUtil.getJobLogger().warn("JobWorker, Completed to sned message to job channel");
            }
        }
    }

    private MessageChannel findChannel(String jobChannelId) {
        ApplicationContext context = ApplicationContextProvider.getContext();
        if (context == null) {
            LoggerUtil.getJobLogger().error(String.format("JobWorker, Cannot get the application context, to startup job %s", jobChannelId));
            return null;
        }

        Object channel = context.getBean(jobChannelId);
        if (channel instanceof MessageChannel) {
            return (MessageChannel) channel;
        }
        else {
            LoggerUtil.getJobLogger().error(String.format("JobWorker, Cannot get the message bean, to startup job %s", jobChannelId));
            return null;
        }
    }
}

JobManagerService

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public final class JobManagerService {
    private BlockingQueue<JobRunnerRequest> jobRequestQueue = new LinkedBlockingQueue<JobRunnerRequest>();
    private static volatile  JobManagerService jobManagerInstnce;
    private static Object objSyncLocker = new Object();

    private JobManagerService() {
    }

    private void startupWorker() {
        new Thread(new JobWorker()).start();
    }

    public static JobManagerService getJobManager() {
        if (jobManagerInstnce == null) {
            synchronized (objSyncLocker) {
                if (jobManagerInstnce == null) {
                    jobManagerInstnce = new JobManagerService();
                    jobManagerInstnce.startupWorker();
                }
            }
        }
        return jobManagerInstnce;
    }

    public void addRequest(JobRunnerRequest request) {
        try {
            jobRequestQueue.put(request);
        }
        catch (InterruptedException e) {
            LoggerUtil.getJobLogger().warn(e.getMessage(), e);
        }
    }

    public JobRunnerRequest takeRequest() {
        try {
            return jobRequestQueue.take();
        }
        catch (InterruptedException e) {
            LoggerUtil.getJobLogger().warn(e.getMessage(), e);
            return null;
        }
    }
}

ApplicatonContextProvider

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class ApplicationContextProvider implements ApplicationContextAware {

    private static volatile ApplicationContext ctx;

    public static ApplicationContext getContext() {
        return ctx;
    }

    private static synchronized void setContext(ApplicationContext applicationContext) {
        ctx = applicationContext;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext){
        setContext(applicationContext);
    }
}

Spring Integration - 自动轮询发送手机短信

时间: 2024-11-10 13:00:49

Spring Integration - 自动轮询发送手机短信的相关文章

JAVA发送手机短信

<p><span>JAVA发送手机短信,流传有几种方法:(1)使用webservice接口发送手机短信,这个可以使用sina提供的webservice进行发送,但是需要进行注册;(2)使用短信mao的方式进行短信的发送,这种方式应该是比较的常用,前提是需要购买硬件设备,呵呵;</span></p> import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient

简单实现发送手机短信

C#简单实现发送手机短信 偶然想起,像编写一个从电脑向手机发送短信的程序,从网上查找到有三种方式:(1)使用webservice接口发送手机短信,这个可以使用sina提供的webservice进行发送,但是需要进行注册;(2)使用短信mao的方式进行短信的发送,这种方式应该是比较的常用,前提是需要购买硬件设备,这个就不考虑了(3)使用中国网建提供的SMS短信平台,但是,用完几条免费的后,就要收费了. 首先,我用C#实现第一种方法,发现总是错误,这个不解,后来从网上查找原因,有的说,新浪这个功能已

C#_发送手机短信

偶然想起,像编写一个从电脑向手机发送短信的程序,从网上查找到有三种方式:(1)使用webservice接口发送手机短信,这个可以使用sina提供的webservice进行发送,但是需要进行注册;(2)使用短信mao的方式进行短信的发送,这种方式应该是比较的常用,前提是需要购买硬件设备,这个就不考虑了(3)使用中国网建提供的SMS短信平台,但是,用完几条免费的后,就要收费了. 首先,我用C#实现第一种方法,发现总是错误,这个不解,后来从网上查找原因,有的说,新浪这个功能已经不用了,我也不太清楚,就

利用java实现的一个发送手机短信的小例子

今天闲来无事,在微博上看到一个关于用java实现的一个发送手机短信的程序,看了看,写的不太相信,闲的没事,把他整理下来,以后可能用得着 JAVA发送手机短信,流传有几种方法:(1)使用webservice接口发送手机短信,这个可以使用sina提供的webservice进行发送,但是需要进行注册;(2)使用短信mao的方式进行短信的发送,这种方式应该是比较的常用,前提是需要购买硬件设备,呵呵(3)使用中国网建提供的SMS短信平台(申请账号地址:http://sms.webchinese.cn/de

第一次发博,发个简单的Java程序发送手机短信验证

最近在准备一个项目,想的登录时候用手机验证,就通过上网查阅了一下手机验证的实现方法,原来超级简单,下面将一步一步介绍. 1.去中国网建注册一个账号密码,首次注册送五条免费短信和3条免费彩信.具体的网址是 http://www.smschinese.cn/api.shtml 2.注册完成之后进去查看给你的短信秘钥 3.有了这个秘钥就超级简单了,导入jar包,下面的代码第一个基本不用该,直接粘贴,第二个改成自己的信息就可以了 1 package duanxinyanzheng; 2 3 4 impo

ava调用WebService接口实现发送手机短信验证码功能

二:前台的注册页面的代码:reg.jsp  <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags"%> <%@page import="cn.gov.csrc.base.action.FindAllData&

C#批量发送手机短信——云通讯平台

云通讯平台:http://www.yuntongxun.com 1.在云通讯平台建短信模板 2.通过上述账号信息编辑如下: //phoneNumber 发送到的手机号 content 短信内容 public ResponseMessage SmsSend(string phoneNumber, string content) { CCPRestSDK api = new CCPRestSDK(); //ip格式如下,不带https:// bool isInit = api.init("app.c

国都企信通短信平台发送手机短信的python脚本一例

一年前,由于工作需要,给以色列的同事解释一下国都短信平台的短信发送格式,本来不懂python的我硬着头皮写了一个sample,比较粗,能用,但不优美,希望以后学会python能改得像我同事写的那么优雅 #!/usr/bin/python #coding:utf8 import sys,httplib,urllib,urllib2 import xml.etree.ElementTree as ET sms=u'测试短信内容[签名部分]' #input message here with '' g

django之集成阿里云通信(发送手机短信验证码)

python3 + django2.0 集成 "阿里云通信" 服务: (SDK文档地址:https://help.aliyun.com/document_detail/55491.html?spm=5176.10629532.106.3.2fe01cbeAp0iFO) 步骤1: 在阿里云 "短信服务" 中创建一个签名 步骤2: 在阿里云 "短信服务" 中创建一个短信模板 步骤3: 下载阿里云 "短信服务" SDK 步骤4: 在