PCService.java
1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package cn.com.glsx.notify.modules.service;
import cn.com.glsx.notify.modules.model.PCTemplateMessageDTO;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Service;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
import javax.annotation.Resource;
/**
* @author payu
*/
@Slf4j
@Service
public class PCService {
@Resource
private KafkaTemplate<String, String> kafkaTemplate;
public void sendMessage(PCTemplateMessageDTO messageDTO) {
// 发送消息,json 作为消息体
ListenableFuture<SendResult<String, String>> future = kafkaTemplate.send(messageDTO.getApplication() + "_topic", JSONObject.toJSONString(messageDTO));
// 监听回调
future.addCallback(new ListenableFutureCallback<SendResult<String, String>>() {
@Override
public void onSuccess(SendResult<String, String> result) {
log.info("## Send message success ...");
}
@Override
public void onFailure(Throwable throwable) {
log.error("## Send message fail ...{}", throwable.getMessage());
}
});
}
}