4 changed files with 148 additions and 31 deletions
@ -0,0 +1,66 @@ |
|||
package com.ccsens.util.message; |
|||
|
|||
import cn.hutool.core.collection.CollectionUtil; |
|||
import com.alibaba.fastjson.JSONObject; |
|||
import com.ccsens.util.bean.message.common.InMessage; |
|||
import lombok.Data; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.HashSet; |
|||
import java.util.List; |
|||
import java.util.Set; |
|||
|
|||
@Data |
|||
public class BaseMessageDto { |
|||
@Data |
|||
public static class MessageUser { |
|||
private Long id; |
|||
private Long userId; //本质上是authId //20190507 本质上是userId
|
|||
private String nickname; |
|||
private String avatarUrl; |
|||
private boolean hasRead; |
|||
public MessageUser(){ |
|||
hasRead = false; |
|||
} |
|||
public MessageUser(Long userId){ |
|||
hasRead = false; |
|||
this.userId = userId; |
|||
} |
|||
public MessageUser(Long id,Long userId,String nickname,String avatarUrl){ |
|||
this(); |
|||
this.id = id; |
|||
this.userId = userId; |
|||
this.nickname = nickname; |
|||
this.avatarUrl = avatarUrl; |
|||
} |
|||
|
|||
public static List<MessageUser> userIdToUsers(List<Long> userIds) { |
|||
List<MessageUser> users = new ArrayList<>(); |
|||
userIds.forEach(userId ->{ |
|||
users.add(new MessageUser(userId)); |
|||
}); |
|||
return users; |
|||
} |
|||
} |
|||
|
|||
private Long time; |
|||
private String type; |
|||
private String event; |
|||
private Long projectId; |
|||
private MessageUser sender; |
|||
private List<MessageUser> receivers; |
|||
// private Object data;
|
|||
|
|||
public Set<String> receiversTransTos() { |
|||
Set<String> tos = new HashSet<>(); |
|||
if (CollectionUtil.isEmpty(receivers)) { |
|||
return tos; |
|||
} |
|||
receivers.forEach(receiver -> { |
|||
InMessage.To to = new InMessage.To(receiver.getUserId()); |
|||
tos.add(JSONObject.toJSONString(to)); |
|||
}); |
|||
|
|||
return tos; |
|||
} |
|||
} |
@ -0,0 +1,35 @@ |
|||
package com.ccsens.util.message; |
|||
|
|||
import lombok.Data; |
|||
import lombok.EqualsAndHashCode; |
|||
|
|||
/** |
|||
* @author 逗 |
|||
*/ |
|||
|
|||
@EqualsAndHashCode(callSuper = true) |
|||
@Data |
|||
public class RecoveryWithStartRecipe extends BaseMessageDto{ |
|||
/**切换项目*/ |
|||
public static final String SWITCHOVER_PROJECT = "switchoverProject"; |
|||
|
|||
@lombok.Data |
|||
public static class Data{ |
|||
private Long projectId; |
|||
} |
|||
|
|||
private Data data; |
|||
|
|||
public RecoveryWithStartRecipe(){ |
|||
setType(SWITCHOVER_PROJECT); |
|||
setEvent(SWITCHOVER_PROJECT); |
|||
setTime(System.currentTimeMillis()); |
|||
} |
|||
|
|||
public RecoveryWithStartRecipe(Long projectId){ |
|||
this(); |
|||
Data d = new Data(); |
|||
d.setProjectId(projectId); |
|||
setData(d); |
|||
} |
|||
} |
@ -0,0 +1,40 @@ |
|||
package com.ccsens.util.message; |
|||
|
|||
import com.ccsens.util.JacksonUtil; |
|||
import com.ccsens.util.bean.message.common.InMessage; |
|||
import com.ccsens.util.bean.message.common.MessageConstant; |
|||
import com.ccsens.util.bean.message.common.MessageRule; |
|||
import com.ccsens.util.config.RabbitMQConfig; |
|||
import com.fasterxml.jackson.core.JsonProcessingException; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.amqp.core.AmqpTemplate; |
|||
import org.springframework.amqp.rabbit.core.RabbitTemplate; |
|||
|
|||
import java.util.Set; |
|||
|
|||
/** |
|||
* @author 逗 |
|||
*/ |
|||
@Slf4j |
|||
public class SwitchoverProjectUtil { |
|||
|
|||
public static void switchoverProject(Set<String> userIdSet,Long projectId) throws JsonProcessingException { |
|||
AmqpTemplate amqpTemplate = new RabbitTemplate(); |
|||
log.info("切换项目:{}--{}",projectId,userIdSet); |
|||
//设定发送规则
|
|||
MessageRule messageRule = MessageRule.defaultRule(MessageConstant.DomainType.User); |
|||
messageRule.setAckRule(MessageRule.AckRule.NONE); |
|||
messageRule.setOfflineDiscard((byte) 1); |
|||
//生成消息
|
|||
RecoveryWithStartRecipe recoveryWithStartRecipe = new RecoveryWithStartRecipe(projectId); |
|||
//封装成inMessage
|
|||
InMessage inMessage = new InMessage(); |
|||
inMessage.setToDomain(MessageConstant.DomainType.User); |
|||
inMessage.setTos(userIdSet); |
|||
inMessage.setData(JacksonUtil.beanToJson(recoveryWithStartRecipe)); |
|||
inMessage.setRule(messageRule); |
|||
log.info("发送切换项目信息:{}",inMessage); |
|||
amqpTemplate.convertAndSend(RabbitMQConfig.MESSAGE_QUEUE_NAME, |
|||
JacksonUtil.beanToJson(inMessage)); |
|||
} |
|||
} |
Loading…
Reference in new issue