Spring Cloud Stream messages and message-driven @SendTo downgrade

Programmers Reference DD Gangster article, learn their new demo, due to the need acknowledgment messages, see @SendTo this annotation can be achieved, let's start learning demo, two new projects cloud-stream-consumerthe consumer side and cloud-stream-consumerthe production side

public interface StreamReceive {

    @Input("MQRece")
    SubscribableChannel mqReceive();
}

Adding an StreamReceiveinterface that defines @inputthe channel

@Component
@Slf4j
public class ReceiveListener {

    @StreamListener("MQRece")
    public byte[] receive(byte[] bytes){
        log.info("接受消息:"+new String(bytes));
        return "ok".getBytes();
    }

}

Add a message listener to receive messages defined asbyte[]

Add application.propertiesprofile information

spring.cloud.stream.rocketmq.binder.namesrv-addr= 192.168.211.11:9876
spring.cloud.stream.bindings.MQRece.destination=message-topic
spring.cloud.stream.bindings.MQRece.group=rece-group
server.port=19999

Add theme MQRece channel message-topic, group namerece-group

Stream this client consumption is complete, this section need to @SendTo notes with them, need to create a MessageChannel be generated news

public interface MsgBackPush {
    
    @Output("back-push")
    MessageChannel backPush();
}

Then ReceiveListeneradd @SendTo

@Component
@Slf4j
public class ReceiveListener {

    @StreamListener("MQRece")
    @SendTo("back-push")
    public byte[] receive(byte[] bytes){
        log.info("接受消息:"+new String(bytes));
        return "ok".getBytes();
    }

}

New channel configurationapplication.properties

spring.cloud.stream.bindings.back-push.destination=back-topic
spring.cloud.stream.bindings.back-push.group=back-group

Remember to add the class to start SpringBoot @EnableBinding(value = {StreamReceive.class,MsgBackPush.class})

@SpringBootApplication
@EnableBinding(value = {StreamReceive.class,MsgBackPush.class})
public class CloudStreamConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(CloudStreamConsumerApplication.class, args);
    }

}

This, cloud-stream-consumer this demo is complete

Then take a look cloud-stream-producer

public interface StreamPush {

    @Output("MQPush")
    MessageChannel mqPush();
}

Define a channel name MQPush, a message produced

public interface ProducerReceive {


    @Input("producer-receive")
    SubscribableChannel producerReceive();
}

Define a channel called producer-receive, a receipt message consumption

@Component
@Slf4j
public class ProducerListener {
    @StreamListener("producer-receive")
    public void producerReceive(byte[] bytes){
        log.info("come back message:"+new String(bytes));
    }
}

DETAILED receipt message handling logic, look atapplication.properties

spring.cloud.stream.rocketmq.binder.namesrv-addr= 192.168.214.191:9876
spring.cloud.stream.bindings.MQPush.destination=message-topic
spring.cloud.stream.bindings.MQPush.group=push-group


spring.cloud.stream.bindings.producer-receive.destination=back-topic
spring.cloud.stream.bindings.producer-receive.group=back-group


server.port=20000

Set topic for the channel and group, create a new interface to test the results of Http

@SpringBootApplication
@EnableBinding(value = {StreamPush.class,ProducerReceive.class})
@RestController
public class CloudStreamProducerApplication {

    public static void main(String[] args) {
        SpringApplication.run(CloudStreamProducerApplication.class, args);
    }

    @Autowired
    private StreamPush streamPush;

    @GetMapping("/sendMessage")
    public String sendMessage(){
        streamPush.mqPush().send(MessageBuilder.withPayload("message body".getBytes()).build());
        return "ok";
    }
}

Access http://localhost:20000/sendMessage, the following results of FIG.

cloud-stream-consumer log output

file

cloud-stream-producer output log

file

Learn @ServiceActivator this annotation, the above project cloud-stream-consumerReceiveListener class added

@Component
@Slf4j
public class ReceiveListener {

    @StreamListener("MQRece")
    @SendTo("back-push")
    public byte[] receive(byte[] bytes){
        log.info("接受消息:"+new String(bytes));
                // 抛出异常
        if(1==1){
            throw new RuntimeException("Message consumer failed!");
        }
        return "ok".getBytes();
    }

    @Autowired
    private MsgBackPush msgBackPush;

    @ServiceActivator(inputChannel = "message-topic.rece-group.errors")
    public void error(Message<?> message){
        log.info("消费者消费消息失败:"+message);
        msgBackPush.backPush().send(MessageBuilder.withPayload("消息消费失败".getBytes()).build());
    }
}

Specifies a channel by using @ServiceActivator (inputChannel = "test-topic.stream-exception-handler.errors") handles the mapping error. Wherein, inputChannel configuration correspondence is as follows:

  • message-topic: message channel corresponding to a target (destination, i.e.: spring.cloud.stream.bindings.MQRece.destination Configuration)
  • rece-group: message channel corresponding consumer groups (group, i.e.: spring.cloud.stream.bindings.MQRece.group Configuration)

Access http://localhost:20000/sendMessage, the following results of FIG.

cloud-stream-consumer log output

file

cloud-stream-producer output log

file

Personal contact QQ: 944484545, welcome to join, to share learning is a happy thing

Guess you like

Origin www.cnblogs.com/hy-xiaobin/p/12175120.html