消息的生产者

MessageSender

@EnableBinding(Source.class)
@Component
public class MessageSender {

   @Autowired
    private MessageChannel output;

    //发送消息
    public void send(Object obj){
        myoutput.send(MessageBuilder.withPayload(obj).build());
    }
}

启动类

@SpringBootApplication
public class ProducerApplication{
    public static void main(String[] args) {

        SpringApplication.run(ProducerApplication.class);

    }
}

测试类

@SpringBootTest(classes = ProducerApplication.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class ProducerTest {

    @Autowired
    private MessageSender messageSender;

    @Test
    public void testSend(){
        messageSender.send("hello老哥,我是老刘");
    }
}

消息的消费者

MessageListener

@Component
@EnableBinding(Sink.class)
public class MessageListener {

     @StreamListener(Sink.INPUT)
    public void input(String message){
        System.out.println("获取到的消息是:"+message);
    }
}

启动类

@SpringBootApplication
public class ConsumerApplication{
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class);
    }

}
作者:Jeebiz  创建时间:2023-03-30 11:47
最后编辑:Jeebiz  更新时间:2024-11-01 10:06