使用rocketMQ进行通信

需求:确定两个平台之间能够正常通信,以及业务模块在绑定授权码后像控制平台发送一条消息
代码实现:
application.yml

rocketmq:
  name-server: 199.199.199.199:9876;199.199.199.199:9876
  #name-server: 127.0.0.1:9876
  producer:
    group: sveco-project-be-parent-dev
    send-message-timeout: 30000
    #发送ams开通/关闭服务结果topic
    topicServiceSwitchResult: 004_ams_service_switch_result
  consumer:
    #接收ams申请开通/关闭服务topic
    topicServiceSwitch: 004_ams_project_service_switch

xml

<dependency>
	<groupId>org.apache.rocketmq</groupId>
	<artifactId>rocketmq-spring-boot-starter</artifactId>
	<version>2.1.0</version>
</dependency>

ServiceSwitchConsumer

import com.alibaba.fastjson.JSONObject;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Slf4j
@Service
@RocketMQMessageListener(topic = "${rocketmq.consumer.topicServiceSwitch}", consumerGroup = "${rocketmq.producer.group}")
public class ServiceSwitchConsumer implements RocketMQListener<MessageExt> {

    @Autowired
    ServiceSwitchProducer serviceSwitchProducer;

    @SneakyThrows
    @Override
    public void onMessage(MessageExt messageExt) {
        String s = new String(messageExt.getBody(), "UTF-8");
        log.debug("收到消息:{}", s);

        JSONObject messageJson = JSONObject.parseObject(s);
        //避免空指针
        JSONObject serviceSwitchJson = Optional.ofNullable(messageJson).map(p -> p.getJSONObject("data"))
                .orElse(new JSONObject());

        serviceSwitchJson.put("respCode", "0");
        serviceSwitchJson.put("respMsg", "成功");
        serviceSwitchJson.put("respMsgEn", "SUCCESS");
        serviceSwitchJson.put("errorCode", "0");

        serviceSwitchProducer.sendMsg(serviceSwitchJson);
    }
}

ServiceSwitchProducer

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import java.util.Optional;
import java.util.UUID;

@Slf4j
@Service
public class ServiceSwitchProducer {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 组名
     */
    @Value("${rocketmq.producer.topicServiceSwitchResult}")
    String serviceSwitchResultTopic;

    /**
     * 发送消息
     *
     * @param serviceSwitchJson
     */
    public void sendMsg(JSONObject serviceSwitchJson) {

        log.debug("开始发送服务开/关结果消息");
        String spCategoryName = Optional.ofNullable(serviceSwitchJson).map(m -> m.getString("spCategoryName"))
                .orElse("video");

        JSONObject headers = new JSONObject();
        headers.put("messageTraceId", UUID.randomUUID());
        headers.put("messageSource", "project-service");
        headers.put("sourceTimeStamp", System.currentTimeMillis());
        headers.put("producerName", "project-service");
        headers.put("project_id", "project_id");

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", serviceSwitchJson);
        jsonObject.put("headers", headers);

        Message<?> message = MessageBuilder.withPayload(jsonObject).build();

        log.debug("返回结果报文:{}", jsonObject);
        SendResult sendResult = rocketMQTemplate.syncSend(serviceSwitchResultTopic + ":" + spCategoryName, message);

        log.debug("发送结果:{}", sendResult);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值