1
votes

I have a rabbitmq exchange [test-exchange] which is a direct exchange that is binded to queue [test-queue] using routing key [test-routing-key].

I am trying to publish messages to this exchange using spring cloud stream but messages are not reaching the queue

My dependency in pom.xml

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>

Spring cloud used is Edgware.RELEASE

My source file code is

public interface DemoSource {

    @Output("demoChannel")
    MessageChannel userChannel();

}

My application file code is

@SpringBootApplication
@EnableBinding(DemoSource.class)
public class PublisherApplication implements CommandLineRunner{

    @Autowired
    DemoSource demoSource;

    public static void main(String[] args) {
        SpringApplication sp = new SpringApplication(PublisherApplication.class);
        sp.run(args);

    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("Sending message...");
        User user = new User();
        user.setUserId("testId");
        user.setUserName("testName");
        demoSource.userChannel().send(MessageBuilder.withPayload(user).build());

    }

}

My application.properties file is

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

spring.cloud.stream.bindings.demoChannel.destination=test-exchange
spring.cloud.stream.bindings.demoChannel.group=test-routing-key
spring.cloud.stream.bindings.demoChannel.producer.bindingRoutingKey=test-routing-key
spring.cloud.stream.bindings.demoChannel.producer.routing-key-expression= 'test-routing-key'
spring.cloud.stream.rabbit.bindings.demoChannel.producer.exchange-type=direct
spring.cloud.stream.rabbit.bindings.demoChannel.producer.declare-exchange=false
spring.cloud.stream.default.contentType=application/json

1
Have you actually tried to post a Message to the exchange directly and see if it reaches the queue? I am simply asking if you can go through process of elimination as we can not see your actual Rabbit configuration.Oleg Zhurakousky
Yes, it is working if my directly post a message to the exchange i.e it reaches the queue. I have also tried with spring boot rabbit template which works, it doesn't work with spring cloudPraveen Rajan
Have you got any solution ?Nikhil

1 Answers

1
votes

I'm super late to this party, but your @Output("demoChannel") is explicitly named, so when you want to publish, you want

demoSource.demoChannel().send(MessageBuilder.withPayload(user).build());

instead of your current

demoSource.userChannel().send(MessageBuilder.withPayload(user).build());