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客户端接口,下面我把我在项目中用到的记录一下,以便给大家提供个思路,由于本人的文采有限, ...
随机推荐
- Yii里获取当前controller和action的id
Yii里获取当前controller和action的id 在控制器里$name = $this->getId(); // controller$name = $action->id; ...
- CSS命名格式
CSS样式命名整理页面结构 容器: container/wrap整体宽度:wrapper页头:header内容:content页面主体:main页尾:footer导航:nav侧栏:sidebar栏目: ...
- NPOI基本操作XLS
using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using Sys ...
- NYOJ题目845无主之地1
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAskAAAKbCAIAAACIEYBGAAAgAElEQVR4nO3dvXKkPLe38X0Szn0gjv
- 局域网聊天Chat(马士兵视频改进版)
Github地址: https://github.com/BenDanChen/Chat Chat 小小的聊天系统,主要是跟着网上的马士兵老师的公开视频然后再自己反思有什么地方需要改进的地方,然后大体 ...
- 三、jQuery--jQuery基础--jQuery基础课程--第9章 jQuery 常用插件
1.表单验证插件——validate 该插件自带包含必填.数字.URL在内容的验证规则,即时显示异常信息,此外,还允许自定义验证规则,插件调用方法如下:$(form).validate({option ...
- Spring学习笔记—Spring之旅
1.Spring简介 Spring是一个开源框架,最早由Rod Johnson创建,并在<Expert One-on-One:J2EE Design and Development> ...
- WPA: 4-Way Handshake failed - pre-shared key may be incorrect
生成psk网址: https://www.wireshark.org/tools/wpa-psk.html 相关 bug: 重点 关注 : https://en.community.sonos.com ...
- Delphi编程建议遵守的规范2---命名规范
1.1.形参命名建议 所有形参的名称都应当表达出它的用途.如果合适的话,形参的名称最好以字母a 为前缀,例如: procedure SomeProc(aUserName:string; aUserAg ...
- ASP.NET多线程下使用HttpContext.Current为null解决方案 2015-01-22 15:23 349人阅读 评论(0) 收藏
问题一:多线程下获取文件绝对路径 当我们使用HttpContext.Current.Server.MapPath(strPath)获取绝对路径时HttpContext.Current为null,解决办 ...