common中增加消息发布客户端
This commit is contained in:
@ -0,0 +1,24 @@
|
|||||||
|
package com.chinaunicom.mall.ebtp.common.bizmessage.client;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
|
||||||
|
import com.chinaunicom.mall.ebtp.common.bizmessage.config.BizMessageClientConfig;
|
||||||
|
import com.chinaunicom.mall.ebtp.common.bizmessage.entity.MessageRaw;
|
||||||
|
import com.chinaunicom.mall.ebtp.common.bizmessage.entity.MessageResult;
|
||||||
|
import com.chinaunicom.mall.ebtp.common.bizmessage.fallback.BizMessageClientFallback;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 消息客户端
|
||||||
|
*
|
||||||
|
* @author ajaxfan
|
||||||
|
*/
|
||||||
|
@FeignClient(name = "biz-message-client", url = "${core-service-ebtp-extend.biz.message}", configuration = BizMessageClientConfig.class, fallback = BizMessageClientFallback.class)
|
||||||
|
public interface BizMessageFeignClient {
|
||||||
|
|
||||||
|
@PostMapping(value = "/v1/producer/")
|
||||||
|
Optional<MessageResult> postMessage(MessageRaw raw);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package com.chinaunicom.mall.ebtp.common.bizmessage.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import com.chinaunicom.mall.ebtp.common.bizmessage.error.BizMessageErrorDecoder;
|
||||||
|
|
||||||
|
import feign.codec.ErrorDecoder;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class BizMessageClientConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ErrorDecoder bizMessageErrorDecoder() {
|
||||||
|
return new BizMessageErrorDecoder();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
package com.chinaunicom.mall.ebtp.common.bizmessage.entity;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.experimental.Accessors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 消息原型
|
||||||
|
*
|
||||||
|
* @author ajaxfan
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@Accessors(chain = true)
|
||||||
|
public class MessageRaw {
|
||||||
|
|
||||||
|
/* 消息标题 */
|
||||||
|
private String title;
|
||||||
|
|
||||||
|
/* 消息类别 */
|
||||||
|
private String category;
|
||||||
|
|
||||||
|
/* 消息模板编码 */
|
||||||
|
private String templateCode;
|
||||||
|
|
||||||
|
/* 消息内容对象 */
|
||||||
|
private Map<String, Object> body;
|
||||||
|
|
||||||
|
/* 消息附加参数 */
|
||||||
|
private Map<String, Object> extra;
|
||||||
|
|
||||||
|
/* 授权用户列表 */
|
||||||
|
private List<String> users;
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package com.chinaunicom.mall.ebtp.common.bizmessage.entity;
|
||||||
|
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class MessageResult {
|
||||||
|
|
||||||
|
/* 消息编码 */
|
||||||
|
private String msgId;
|
||||||
|
|
||||||
|
/* 消息标题 */
|
||||||
|
private String title;
|
||||||
|
|
||||||
|
/* 消息类别 */
|
||||||
|
private String category;
|
||||||
|
|
||||||
|
/* 消息内容 */
|
||||||
|
private String content;
|
||||||
|
|
||||||
|
/* 消息路由 */
|
||||||
|
private String url;
|
||||||
|
|
||||||
|
/* 消息参数 */
|
||||||
|
private String params;
|
||||||
|
|
||||||
|
/* 消息发布时间 */
|
||||||
|
private Timestamp createtime;
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.chinaunicom.mall.ebtp.common.bizmessage.error;
|
||||||
|
|
||||||
|
import feign.Response;
|
||||||
|
import feign.codec.ErrorDecoder;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 服务端异常映射
|
||||||
|
*
|
||||||
|
* @author Ajaxfan
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class BizMessageErrorDecoder implements ErrorDecoder {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Exception decode(String methodKey, Response response) {
|
||||||
|
String message = response.body().toString();
|
||||||
|
log.error("Publish Message error: {}", message);
|
||||||
|
|
||||||
|
return new Exception(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.chinaunicom.mall.ebtp.common.bizmessage.exception;
|
||||||
|
|
||||||
|
public class BadRequestException extends Exception {
|
||||||
|
private static final long serialVersionUID = -5514128965548804194L;
|
||||||
|
|
||||||
|
public BadRequestException() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public BadRequestException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BadRequestException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "BadRequestException: " + getMessage();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.chinaunicom.mall.ebtp.common.bizmessage.exception;
|
||||||
|
|
||||||
|
public class NotFoundException extends Exception {
|
||||||
|
private static final long serialVersionUID = 562465843254417453L;
|
||||||
|
|
||||||
|
public NotFoundException() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public NotFoundException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public NotFoundException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "NotFoundException: " + getMessage();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.chinaunicom.mall.ebtp.common.bizmessage.fallback;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import com.chinaunicom.mall.ebtp.common.bizmessage.client.BizMessageFeignClient;
|
||||||
|
import com.chinaunicom.mall.ebtp.common.bizmessage.entity.MessageResult;
|
||||||
|
import com.chinaunicom.mall.ebtp.common.bizmessage.entity.MessageRaw;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class BizMessageClientFallback implements BizMessageFeignClient {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<MessageResult> postMessage(MessageRaw raw) {
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,77 @@
|
|||||||
|
package com.chinaunicom.mall.ebtp.common.bizmessage.publish;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
|
import com.chinaunicom.mall.ebtp.common.bizmessage.client.BizMessageFeignClient;
|
||||||
|
import com.chinaunicom.mall.ebtp.common.bizmessage.entity.MessageResult;
|
||||||
|
import com.chinaunicom.mall.ebtp.common.bizmessage.entity.MessageRaw;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 消息发布工具
|
||||||
|
*
|
||||||
|
* @author Ajaxfan
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class BizMessagePublisher {
|
||||||
|
|
||||||
|
private @Autowired BizMessageFeignClient client;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 广播消息
|
||||||
|
*
|
||||||
|
* @param tilte 消息标题
|
||||||
|
* @param category 消息类别
|
||||||
|
* @param templateCode 消息模板编码
|
||||||
|
* @param body 消息内容
|
||||||
|
* @param params 消息参数
|
||||||
|
* @return 消息结果
|
||||||
|
*/
|
||||||
|
public Optional<MessageResult> publishToAll(String title, String category, String templateCode, Map<String, Object> body,
|
||||||
|
Map<String, Object> params) {
|
||||||
|
return publishMessage(title, category, templateCode, body, params, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 像指定用户群发布消息
|
||||||
|
*
|
||||||
|
* @param tilte 消息标题
|
||||||
|
* @param category 消息类别
|
||||||
|
* @param templateCode 消息模板编码
|
||||||
|
* @param body 消息内容
|
||||||
|
* @param params 消息参数
|
||||||
|
* @param grantUsers 目标用户列表
|
||||||
|
* @return 消息结果
|
||||||
|
*/
|
||||||
|
public Optional<MessageResult> publishToSpecialUsers(String title, String category, String templateCode,
|
||||||
|
Map<String, Object> body, Map<String, Object> params, List<String> grantUsers) {
|
||||||
|
return publishMessage(title, category, templateCode, body, params, grantUsers);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param title
|
||||||
|
* @param category
|
||||||
|
* @param templateCode
|
||||||
|
* @param body
|
||||||
|
* @param params
|
||||||
|
* @param grantUsers
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private Optional<MessageResult> publishMessage(String title, String category, String templateCode,
|
||||||
|
Map<String, Object> body, Map<String, Object> params, List<String> grantUsers) {
|
||||||
|
Assert.notNull(title, "消息标题为空");
|
||||||
|
Assert.notNull(category, "消息类别编号为空");
|
||||||
|
Assert.notNull(templateCode, "模板编号为空");
|
||||||
|
Assert.notNull(body, "消息内容为空");
|
||||||
|
Assert.notNull(params, "消息参数为空");
|
||||||
|
|
||||||
|
return client.postMessage(new MessageRaw().setTitle(title).setCategory(category)
|
||||||
|
.setTemplateCode(templateCode).setBody(body).setExtra(params).setUsers(grantUsers));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
2
uboot-common/src/main/resources/application.properties
Normal file
2
uboot-common/src/main/resources/application.properties
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
core-service-ebtp-extend.biz.message=http://10.242.31.158:18018
|
||||||
|
spring.main.allow-bean-definition-overriding=true
|
Reference in New Issue
Block a user