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 - 自动轮询发送手机短信的更多相关文章
- SNF开发平台WinForm之十二-发送手机短信功能调用-金笛-SNF快速开发平台3.3-Spring.Net.Framework
1.调用前组装参数 2.调用发送信息服务脚本 .调用前组装参数: BaseSendTaskEntity entity = new BaseSendTaskEntity(); entity.Mess ...
- 利用java实现的一个发送手机短信的小例子
今天闲来无事,在微博上看到一个关于用java实现的一个发送手机短信的程序,看了看,写的不太相信,闲的没事,把他整理下来,以后可能用得着 JAVA发送手机短信,流传有几种方法:(1)使用webservi ...
- C#简单实现发送手机短信
偶然想起,像编写一个从电脑向手机发送短信的程序,从网上查找到有三种方式:(1)使用webservice接口发送手机短信,这个可以使用sina提供的webservice进行发送,但是需要进行注册;(2) ...
- JAVA发送手机短信
<p><span>JAVA发送手机短信,流传有几种方法:(1)使用webservice接口发送手机短信,这个可以使用sina提供的webservice进行发送,但是需要进行注册 ...
- 装饰者模式的学习(c#) EF SaveChanges() 报错(转载) C# 四舍五入 保留两位小数(转载) DataGridView样式生成器使用说明 MSSQL如何将查询结果拼接成字符串 快递查询 C# 通过smtp直接发送邮件 C# 带参访问接口,WebClient方式 C# 发送手机短信 文件 日志 写入 与读取
装饰者模式的学习(c#) 案例转自https://www.cnblogs.com/stonefeng/p/5679638.html //主体基类 using System;using System.C ...
- 用Java通过串口发送手机短信
用Java通过串口发短信其实很简单,因为有现成的类库供我们使用.有底层的类库,也有封装好一点的类库,下面我介绍一下在 Win32 平台下发送短信的方法. 如果你想用更底层的类库开发功能更强大的应用程序 ...
- 四:java调接口实现发送手机短信验证码功能
1.点击获取验证码之前的样式: 2.输入正确的手机号后点击获取验证码之后的样式: 3.如果手机号已经被注册的样式: 4.如果一个手机号一天发送超过3次就提示不能发送: 二:前台的注册页面的代码:reg ...
- yii2验证密码->手机号码短信发送>手机短信发送频繁问题
<?php namespace frontend\models; use Yii; use yii\base\Model; class ChangeMobileSendRequestForm e ...
- Java调用WebService接口实现发送手机短信验证码功能,java 手机验证码,WebService接口调用
近来由于项目需要,需要用到手机短信验证码的功能,其中最主要的是用到了第三方提供的短信平台接口WebService客户端接口,下面我把我在项目中用到的记录一下,以便给大家提供个思路,由于本人的文采有限, ...
随机推荐
- java jsp调用shell(带参数)脚本并返回值
test.jsp <%@ page language="java" import="java.util.List,java.util.ArrayList,java. ...
- Excel计算一列的和sum(A:A)
在公式中输入=sum(A2:A6),计算的是A列2-6行的和 =sum(A:A)计算的是A列全部的和
- Sightseeing(poj 3463)
题意:给出n个点m条单向边,求最短路的道路条数和比最短路大1的道路条数的和. /* 用Dijkstra更新2*n次,来更新出所有点的最短路和次短路,顺便更新方案数. */ #include<cs ...
- 使用DateUtils和DateFormatUtils处理时间日期转换与SimpleDateFormat的区别
在Apache Commons项目的Lang里面,有两个类:DateUtils和DateFormatUtils,专门用于处理时间日期转换.它们在 org.apache.commons.lang.tim ...
- XSLT教程
XSL 指扩展样式表语言(EXtensible Stylesheet Language), 它是一个 XML 文档的样式表语言. XSLT 指 XSL 转换.即使用 XSLT 将 XML 文档转换为其 ...
- jquery学习笔记----jquery相关的文档
http://tool.oschina.net/apidocs/apidoc?api=jquery http://www.w3school.com.cn/jquery/jquery_ref_event ...
- Delphi之DLL知识学习3---为什么要使用DLL
使用DLL有若干理由,其中有一些前面提到过的.大体说来,使用动态链接库可以共享代码.系统资源,可以隐藏实现的代码或底层的系统例程.设计自定义控件 一.共享代码.资源和数据 前面已经提到,共享代码是创建 ...
- 【PHP自定义显示系统级别的致命错误和用户级别的错误】
使用方法set_error_handler可以自定义用户级别的错误和系统级别的错误信息显示和处理 用户级别的错误使用trigger_error方法产生一个用户级别的错误信息 代码示例: 系统级别的错误 ...
- javascript中求浏览器窗口可视区域兼容性写法
1.浏览器窗口可视区域大小 1.1 对于IE9+.Chrome.Firefox.Opera 以及 Safari:• window.innerHeight - 浏览器窗口的内部高度• window. ...
- wp8 入门到精通 测量代码执行时间
Stopwatch time = new Stopwatch(); byte[] target = new byte[size]; for (int j = 0; j < size; j++) ...