Commit 8331bdb9 authored by 杨锐's avatar 杨锐

sendBlackHoleMessage

parent 46fedc54
package cn.quantgroup.xyqb.config.mq;
import cn.quantgroup.tech.brave.service.ITechRabbitBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
/**
* Date: 2020/1/15
* Time: 下午2:20
*
* @author: yangrui
*/
@Slf4j
@Configuration
public class RegisteredNotifyBlackHoleMqConfig {
@Value("${registered.notify.black.hole.rabbitmq.connection.host}")
private String host;
@Value("${registered.notify.black.hole.rabbitmq.connection.port}")
private Integer port;
@Value("${registered.notify.black.hole.rabbitmq.connection.user}")
private String user;
@Value("${registered.notify.black.hole.rabbitmq.connection.password}")
private String password;
@Value("${registered.notify.black.hole.rabbitmq.connection.virtual-host}")
private String virtualHost;
@Resource
private ITechRabbitBuilder techRabbitBuilder;
@Bean(name = "registeredNotifyBlackHoleFactory")
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host, port);
connectionFactory.setChannelCacheSize(1024);
connectionFactory.setCacheMode(CachingConnectionFactory.CacheMode.CONNECTION);
connectionFactory.setChannelCacheSize(180 * 1000);
connectionFactory.setConnectionCacheSize(1024);
connectionFactory.setUsername(user);
connectionFactory.setPassword(password);
connectionFactory.setVirtualHost(virtualHost);
connectionFactory.setPublisherReturns(true);
connectionFactory.setPublisherConfirms(true);
return connectionFactory;
}
@Bean(name = "registeredNotifyBlackHoleRabbitTemplate")
public RabbitTemplate rabbitTemplate(@Qualifier("registeredNotifyBlackHoleFactory") ConnectionFactory connectionFactory) {
return techRabbitBuilder.createRabbitTemplate(connectionFactory);
}
}
package cn.quantgroup.xyqb.config.sentry;
import cn.quantgroup.xyqb.exception.VerificationCodeErrorException;
import io.sentry.Sentry;
import org.hibernate.exception.DataException;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* {@link HandlerExceptionResolver} implementation that will record any exception that a
* Spring {@link org.springframework.web.servlet.mvc.Controller} throws to Sentry. It then
* returns null, which will let the other (default or custom) exception resolvers handle
* the actual error.
*/
public class CustomSentryExceptionResolver implements HandlerExceptionResolver, Ordered {
@Override
public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response,
Object handler,
Exception ex) {
if (ex instanceof VerificationCodeErrorException || ex instanceof DataException) {
return null;
}
Sentry.capture(ex);
// null = run other HandlerExceptionResolvers to actually handle the exception
return null;
}
@Override
public int getOrder() {
// ensure this resolver runs first so that all exceptions are reported
return Integer.MIN_VALUE;
}
}
\ No newline at end of file
...@@ -23,7 +23,7 @@ public class SentryConfig { ...@@ -23,7 +23,7 @@ public class SentryConfig {
@Bean @Bean
public HandlerExceptionResolver sentryExceptionResolver() { public HandlerExceptionResolver sentryExceptionResolver() {
return new io.sentry.spring.SentryExceptionResolver(); return new CustomSentryExceptionResolver();
} }
@Bean @Bean
......
...@@ -3,9 +3,17 @@ package cn.quantgroup.xyqb.event; ...@@ -3,9 +3,17 @@ package cn.quantgroup.xyqb.event;
import cn.quantgroup.xyqb.entity.User; import cn.quantgroup.xyqb.entity.User;
import cn.quantgroup.xyqb.model.UserRegisterParam; import cn.quantgroup.xyqb.model.UserRegisterParam;
import cn.quantgroup.xyqb.util.MqUtils; import cn.quantgroup.xyqb.util.MqUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
/** /**
* 发mq, 目前只有数据可视化分析系统需要这个 * 发mq, 目前只有数据可视化分析系统需要这个
...@@ -15,6 +23,13 @@ import org.springframework.stereotype.Component; ...@@ -15,6 +23,13 @@ import org.springframework.stereotype.Component;
@Component @Component
public class MqRegisteredEventListener implements ApplicationListener<RegisterEvent> { public class MqRegisteredEventListener implements ApplicationListener<RegisterEvent> {
@Value("${registered.notify.black.hole.rabbitmq.connection.exchange}")
private String exchange;
@Value("${registered.notify.black.hole.rabbitmq.connection.routingKey}")
private String routingKey;
@Resource
private RabbitTemplate registeredNotifyBlackHoleRabbitTemplate;
@Override @Override
public void onApplicationEvent(RegisterEvent event) { public void onApplicationEvent(RegisterEvent event) {
UserRegisterParam userRegisterParam = event.getUserRegisterParam(); UserRegisterParam userRegisterParam = event.getUserRegisterParam();
...@@ -30,5 +45,26 @@ public class MqRegisteredEventListener implements ApplicationListener<RegisterEv ...@@ -30,5 +45,26 @@ public class MqRegisteredEventListener implements ApplicationListener<RegisterEv
} }
// 发送注册成功统计消息 // 发送注册成功统计消息
MqUtils.sendRegisterMessage(channelId, dimension, user); MqUtils.sendRegisterMessage(channelId, dimension, user);
sendBlackHoleMessage(user);
}
private void sendBlackHoleMessage(User user) {
LocalDate signDate = LocalDate.now();
String dateStr = signDate.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日"));
int day = signDate.getDayOfMonth();
long templateId = 8;
JSONObject fields = new JSONObject();
fields.put("phoneNo", user.getPhoneNo());
fields.put("genarateDateStr", dateStr);
fields.put("day", day);
JSONObject json = new JSONObject();
json.put("userId", user.getId());
json.put("templateId", templateId);
json.put("fields", fields);
JSONArray array = new JSONArray();
array.add(json);
registeredNotifyBlackHoleRabbitTemplate.convertAndSend(exchange, routingKey, array.toString());
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment