Commit dcab2d6c by 18356073052

扫描群发消息定时任务

1 parent ee09693a
package com.linkwechat.framework.handler;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.wecom.domain.WeCustomer;
import com.linkwechat.wecom.domain.WeGroup;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.type.*;
......@@ -19,15 +21,26 @@ import java.util.List;
*/
//支持的java对象
@SuppressWarnings("all")
@Slf4j
@MappedTypes(value = {WeCustomer.class,WeGroup.class})
@MappedJdbcTypes({JdbcType.VARCHAR})
public class ListTypeHandler<T extends Object> implements TypeHandler<List<T>> {
private List<T> getListByJsonArrayString(String content) {
private List getListByJsonArrayString(String content,String filedName) {
if (StringUtils.isEmpty(content)) {
return new ArrayList<>();
}
return JSON.parseObject(content,new TypeReference<ArrayList>(){});
if(filedName!=null && "customers_info".equals(filedName)){
return JSONObject.parseArray(content, WeCustomer.class);
}
if(filedName!=null && "groups_info".equals(filedName)){
return JSONObject.parseArray(content, WeGroup.class);
}
return null;
}
/**
......@@ -57,17 +70,18 @@ public class ListTypeHandler<T extends Object> implements TypeHandler<List<T>> {
@Override
public List<T> getResult(ResultSet resultSet, String s) throws SQLException {
return getListByJsonArrayString(resultSet.getString(s));
log.info("序列化集合字段:{}",s);
return getListByJsonArrayString(resultSet.getString(s),s);
}
@Override
public List<T> getResult(ResultSet resultSet, int i) throws SQLException {
return getListByJsonArrayString(resultSet.getString(i));
return getListByJsonArrayString(resultSet.getString(i),null);
}
@Override
public List<T> getResult(CallableStatement callableStatement, int i) throws SQLException {
return getListByJsonArrayString(callableStatement.getString(i));
return getListByJsonArrayString(callableStatement.getString(i),null);
}
......
......@@ -25,6 +25,7 @@ import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;
......@@ -54,6 +55,7 @@ public class RyTask {
@Autowired
private IWeCustomerMessageService weCustomerMessageService;
public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) {
System.out.println(StringUtils.format("执行多参方法: 字符串类型{},布尔类型{},长整型{},浮点型{},整形{}", s, b, l, d, i));
}
......@@ -100,17 +102,17 @@ public class RyTask {
}
}
public void WeCustomers(){
public void WeCustomers() {
//查询系统所有客户
List<WeCustomer> cacheList = redisCache.getCacheList(WeConstans.WECUSTOMERS_KEY);
if(CollectionUtils.isEmpty(cacheList)){
if (CollectionUtils.isEmpty(cacheList)) {
List<WeCustomer> customers = weCustomerService.selectWeCustomerList(null);
redisCache.setCacheList(WeConstans.WECUSTOMERS_KEY,customers);
}else {
redisCache.setCacheList(WeConstans.WECUSTOMERS_KEY, customers);
} else {
List<WeCustomer> customers = weCustomerService.selectWeCustomerList(null);
List<WeCustomer> weCustomers = redisCache.getCacheList(WeConstans.WECUSTOMERS_KEY);
if(CollectionUtils.isNotEmpty(weCustomers) && weCustomers.size()<customers.size()){
redisCache.setCacheList(WeConstans.WECUSTOMERS_KEY,customers);
if (CollectionUtils.isNotEmpty(weCustomers) && weCustomers.size() < customers.size()) {
redisCache.setCacheList(WeConstans.WECUSTOMERS_KEY, customers);
}
}
}
......@@ -136,21 +138,48 @@ public class RyTask {
}
public void messageTask(){
/**
* 扫描群发消息定时任务
*/
public void messageTask() {
//获的当前时间的毫秒数
long currentTime = System.currentTimeMillis();
//customerMessageTimeTaskMapper
List<WeCustomerMessageTimeTask> weCustomerMessageTimeTasks = customerMessageTimeTaskMapper.selectWeCustomerMessageTimeTaskGteSettingTime(currentTime);
if(CollectionUtils.isNotEmpty(weCustomerMessageTimeTasks)){
weCustomerMessageTimeTasks.forEach(s->{
try {
weCustomerMessageService.sendMessgae(s.getMessageInfo(), s.getMessageId(),s.getCustomersInfo(),s.getGroupsInfo());
} catch (JsonProcessingException e) {
log.error("定时群发消息处理异常:ex:{}", e);
e.printStackTrace();
}
});
final Semaphore semaphore = new Semaphore(5);
if (CollectionUtils.isNotEmpty(weCustomerMessageTimeTasks)) {
weCustomerMessageTimeTasks.forEach(
s -> {
try {
semaphore.acquire();
if (s.getMessageInfo() != null && s.getMessageId() != null || (s.getMessageInfo().getPushType().equals(WeConstans.SEND_MESSAGE_CUSTOMER)
&& CollectionUtils.isNotEmpty(s.getCustomersInfo())) ||(s.getMessageInfo().getPushType().equals(WeConstans.SEND_MESSAGE_GROUP)
&& CollectionUtils.isNotEmpty(s.getGroupsInfo()))) {
weCustomerMessageService.sendMessgae(s.getMessageInfo(), s.getMessageId(), s.getCustomersInfo(), s.getGroupsInfo());
//更新消息处理状态
customerMessageTimeTaskMapper.updateTaskSolvedById(s.getTaskId());
}
semaphore.release();
} catch (JsonProcessingException | InterruptedException e) {
log.error("定时群发消息处理异常:ex:{}", e);
e.printStackTrace();
}
}
);
}
}
......
......@@ -58,4 +58,9 @@ public class WeCustomerMessageTimeTask extends BaseEntity {
*/
private Long settingTime;
/**
* 0 未解决 1 已解决
*/
private Integer solved;
}
......@@ -30,4 +30,12 @@ public interface WeCustomerMessageTimeTaskMapper extends BaseMapper<WeCustomerMe
*/
int saveWeCustomerMessageTimeTask(WeCustomerMessageTimeTask customerMessageTimeTask);
/**
* 更新群发定时任务处理状态
*
* @param taskId 任务id
* @return int
*/
int updateTaskSolvedById(@Param("taskId") Long taskId);
}
......@@ -37,9 +37,19 @@
#{updateTime}
)
</insert>
<update id="updateTaskSolvedById">
UPDATE we_customer_messageTimeTask
<set>
solved=1
</set>
<where>
task_id=#{taskId}
</where>
</update>
<select id="selectWeCustomerMessageTimeTaskGteSettingTime" resultMap="tasks">
SELECT
task_id,
message_id,
message_info,
customers_info,
......@@ -48,7 +58,7 @@
FROM
we_customer_messageTimeTask
<where>
setting_time lte #{timeMillis}
setting_time <![CDATA[<=]]> #{timeMillis} AND solved=0
</where>
</select>
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!