package com.zzy.web.common.action;
import org.springframework.stereotype.Component;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;
@Component
public class ThreadPoolUtils {
//1.核心线程数
int corePoolSize = 4;
//2.最大线程数
int maximumPoolSize = 8;
//3.线程最大空闲时间
long keepAliveTime = 60;
//4.时间单位
TimeUnit unit = TimeUnit.SECONDS;
//5.任务队列(阻塞式队列)
BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(1);
//6.定义拒绝策略(可选,常用有四种 这里是主线程调用策略 还有默认的AbortPolicy中断策略并抛出个异常,DiscardPolicy丢弃策略不抛出异常,DiscardOldestPolicy弃老策略:丢弃队列最前面的任务)
RejectedExecutionHandler handler = new ThreadPoolExecutor.CallerRunsPolicy();
//7.构建线程工厂(可选,最关键是要给线程一个友好的名字)
ThreadFactory factory = new ThreadFactory() {
//线程名前缀
private String namePrefix = "发送短信线程";
//构建一个线程安全的原子自增自减对象
private AtomicLong atomicLong = new AtomicLong(1);
@Override
public Thread newThread(Runnable r) { //r 为任务
return new Thread(r,namePrefix+atomicLong.getAndIncrement());
}
};
//8.创建线程池
ThreadPoolExecutor pool = new ThreadPoolExecutor
(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,factory,handler);
public void pollsend(String mobile,String content,String username){
pool.execute(new Runnable() {
@Override
public void run() {
SmsUtils.batchSend(SmsUtils.buildCaptchaText(username,content),mobile);
}
});
}
public void pollSendPush(String pushId, String title, String content){
pool.execute(new Runnable() {
@Override
public void run() {
JpushUtil.sendPushRegistrationIdAllMessage(pushId,title,content);
}
});
}
}
package com.zzy.web.common.action;
import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
import java.util.List;
public class JpushUtil {
private static final String API_KEY="*******************";
private static final String Master_Secret="*****************";
/**
* 推送所有平台 用于广播消息 用于管理员使用
*
* @param responseNoticeMessage 消息
*/
public void sendPushAllMessage(Message responseNoticeMessage) {
//生成推送的内容
PushPayload payload = JPushClientPC.buildPushObject_all_message(responseNoticeMessage);
sendPush(payload);
}
/**
* 根据别名推送所有平台
* 一次推送最多 1000 个。
*
* @param userNames 别名
* @param responseNoticeMessage 消息
*/
public void sendPushAliasAllMessage(List<String> userNames, Message responseNoticeMessage) {
//生成推送的内容
PushPayload payload = JPushClientPC.buildPushObject_all_alias_message(userNames, responseNoticeMessage);
sendPush(payload);
}
/**
* 根据别名推送所有平台
*
* @param userName 别名
* @param responseNoticeMessage 消息
*/
public void sendPushAliasAllMessage(String userName, Message responseNoticeMessage) {
//生成推送的内容
PushPayload payload = JPushClientPC.buildPushObject_all_alias_message(userName, responseNoticeMessage);
sendPush(payload);
}
/**
* 根据标签推送所有平台
* 一次推送最多 20 个。
*
* @param tags 标签名
* @param responseNoticeMessage 消息
*/
public void sendPushTagsAllMessage(List<String> tags, Message responseNoticeMessage) {
//生成推送的内容
PushPayload payload = JPushClientPC.buildPushObject_all_tags_message(tags, responseNoticeMessage);
sendPush(payload);
}
/**
* 根据标签推送所有平台
*
* @param tag 标签名
* @param responseNoticeMessage 消息
*/
public void sendPushTagsAllMessage(String tag, Message responseNoticeMessage) {
//生成推送的内容
PushPayload payload = JPushClientPC.buildPushObject_all_tag_message(tag, responseNoticeMessage);
sendPush(payload);
}
/**
* 根据标签推送所有平台
*
* @param registrationIds 标签名
* @param responseNoticeMessage 消息
*/
public void sendPushRegistrationIdsAllMessage(List<String> registrationIds, Message responseNoticeMessage) {
//生成推送的内容
PushPayload payload = JPushClientPC.buildPushObject_all_registrationIds_message(registrationIds, responseNoticeMessage);
sendPush(payload);
}
/**
* 根据登录账号推送所有平台
*
* @param registrationId 标签名
* @param content 消息
*/
public static int sendPushRegistrationIdAllMessage(String registrationId, String title, String content) {
//生成推送的内容
PushPayload payload = JPushClientPC.buildPushObject_all_registrationId_message(registrationId, title, content);
payload.resetOptionsApnsProduction(true);
return push(payload);
}
public static int push(PushPayload payload) {
JPushClient jpushClient = new JPushClient(Master_Secret,API_KEY, null, ClientConfig.getInstance());
try {
PushResult pushResult = jpushClient.sendPush(payload);
if (pushResult.getResponseCode() == 200) {
return 200;
}else if(pushResult.getResponseCode() == 400){
return 400;
}else{
return -12;
}
} catch (APIConnectionException e) {
return -12;
} catch (APIRequestException e) {
return -12;
}
}
/**
* 发送
*
* @param payload
* @return
*/
private PushResult sendPush(PushPayload payload) {
JPushClient jpushClient = new JPushClient(Master_Secret,API_KEY, null, ClientConfig.getInstance());
try {
PushResult result = jpushClient.sendPush(payload);
jpushClient.close();
return result;
} catch (Exception e) {
}
return null;
}
}
package com.zzy.web.common.action;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
import com.alibaba.fastjson.JSON;
import java.util.List;
/**
* 使用别名:
* 用于给某特定用户推送消息。别名,可以近似地被认为,是用户帐号里的昵称。
* 使用标签:
* 用于给某一群人推送消息。
* 标签类似于博客里为文章打上 tag ,即为某资源分类。
*/
public class JPushClientPC {
/**
* 发送所有的消息 全部人
*
* @param responseNoticeMessage
* @return
*/
public static PushPayload buildPushObject_all_message(Message responseNoticeMessage) {
return PushPayload.newBuilder()
.setPlatform(Platform.all())//设置接受的平台
.setAudience(Audience.all())//Audience设置为all,说明采用广播方式推送,所有用户都可以接收到
//.setNotification(Notification.alert(JSON.toJSONString(responseNoticeMessage)))
.setMessage(Message.content(JSON.toJSONString(responseNoticeMessage))) //内部消息不显示,类似web socket
.build();
}
/**
* 根据别名集合 推送
*
* @param userNames 别名List
* @param responseNoticeMessage 消息
* @return
*/
public static PushPayload buildPushObject_all_alias_message(List<String> userNames, Message responseNoticeMessage) {
return PushPayload.newBuilder()
.setPlatform(Platform.all())//设置接受的平台
.setAudience(Audience.alias(userNames))//
//.setNotification(Notification.alert(JSON.toJSONString(responseNoticeMessage)))
.setMessage(Message.content(JSON.toJSONString(responseNoticeMessage))) //内部消息不显示,类似web socket
.build();
}
/**
* 根据别名 推送
*
* @param userName 别名
* @param responseNoticeMessage 消息
* @return
*/
public static PushPayload buildPushObject_all_alias_message(String userName, Message responseNoticeMessage) {
return PushPayload.newBuilder()
.setPlatform(Platform.all())//设置接受的平台
.setAudience(Audience.alias(userName))//
//.setNotification(Notification.alert(JSON.toJSONString(responseNoticeMessage)))
.setMessage(Message.content(JSON.toJSONString(responseNoticeMessage))) //内部消息不显示,类似web socket
.build();
}
/**
* 根据标签集合推送
*
* @param tags 标签List
* @param responseNoticeMessage 消息
* @return
*/
public static PushPayload buildPushObject_all_tags_message(List<String> tags, Message responseNoticeMessage) {
return PushPayload.newBuilder()
.setPlatform(Platform.all())//设置接受的平台
.setAudience(Audience.tag(tags))//
//.setNotification(Notification.alert(JSON.toJSONString(responseNoticeMessage)))
.setMessage(Message.content(JSON.toJSONString(responseNoticeMessage))) //内部消息不显示,类似web socket
.build();
}
/**
* 根据标签推送
*
* @param tag 标签
* @param responseNoticeMessage 消息
* @return
*/
public static PushPayload buildPushObject_all_tag_message(String tag, Message responseNoticeMessage) {
return PushPayload.newBuilder()
.setPlatform(Platform.all())//设置接受的平台
.setAudience(Audience.tag(tag))//
//.setNotification(Notification.alert(JSON.toJSONString(responseNoticeMessage)))
.setMessage(Message.content(JSON.toJSONString(responseNoticeMessage))) //内部消息不显示,类似web socket
.build();
}
/**
* 根据设备ids 推送
*
* @param registrationIds 设备List
* @param responseNoticeMessage 消息
* @return
*/
public static PushPayload buildPushObject_all_registrationIds_message(List<String> registrationIds, Message responseNoticeMessage) {
return PushPayload.newBuilder()
.setPlatform(Platform.all())//设置接受的平台
.setAudience(Audience.registrationId(registrationIds))//
//.setNotification(Notification.alert(JSON.toJSONString(responseNoticeMessage)))
.setMessage(Message.content(JSON.toJSONString(responseNoticeMessage))) //内部消息不显示,类似web socket
.build();
}
/**
* 根据设备id 推送
*
* @param registrationId 设备List
* @param responseNoticeMessage 消息
* @return
*/
public static PushPayload buildPushObject_all_registrationId_message(String registrationId,String title,String content) {
return PushPayload.newBuilder()
.setPlatform(Platform.all())//设置接受的平台
.setAudience(Audience.registrationId(registrationId))//
.setNotification(Notification.alert(content))
.setNotification(Notification.alert(title))
.setMessage(Message.content(content))
.setMessage(Message.content(title))//内部消息不显示,类似web socket
.build();
}
}