Commit 87c2f4b9 by 18356073052

扫描群发消息定时任务

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