0

1) Below is the code for the configuration for rabbitMQ with Spring.

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/rabbit
                        http://www.springframework.org/schema/rabbit/spring-rabbit-1.5.xsd
                        http://www.springframework.org/schema/context 
                        http://www.springframework.org/schema/context/spring-context-4.1.xsd           
                        http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">

    <context:component-scan base-package="com.messaging.impl" />

    <rabbit:connection-factory id="connectionFactory"
            host="${rabbitmq.host}" port="${rabbitmq.port}"
            username="${rabbitmq.username}" password="${rabbitmq.password}"
            channel-cache-size="${rabbitmq.ChannelCacheSize}"
            requested-heartbeat="${rabbitmq.requestedHeartBeat}" />

    <rabbit:template id="amqpTemplate" connection-factory="connectionFactory"
        exchange="${request.exchange}" queue="${request.out.queue}" />

    <rabbit:admin id="rabbitAdmin" connection-factory="connectionFactory" />

    <rabbit:queue name="${request.out.queue}"
            declared-by="rabbitAdmin" />

    <rabbit:listener-container
        connection-factory="connectionFactory">
        <rabbit:listener queues="${request.out.queue}"
                ref="messageSender" />
    </rabbit:listener-container>

    <bean id="messageSender"
          class="com.messaging.impl.MessageSenderImpl" />

    <rabbit:topic-exchange name="${request.exchange}"
            declared-by="rabbitAdmin">
        <rabbit:bindings>
            <rabbit:binding queue="${request.out.queue}"
                    pattern="routingkey.*" />
        </rabbit:bindings>
    </rabbit:topic-exchange>


</beans>

2) Below is the code for Java sender class.

    @Service
    public class MessageSenderImpl implements MessageSender  {
        @Autowired
        private AmqpTemplate amqpTemplate;

        public void sendMessage(String message){

            amqpTemplate.convertAndSend(message);
            System.out.println("message is" +message);
        }
    }

3) pom.xml has the below dependency

    <dependency>
        <groupId>org.springframework.amqp</groupId>
        <artifactId>spring-amqp</artifactId>
        <version>1.5.3.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.amqp</groupId>
        <artifactId>spring-rabbit</artifactId>
        <version>1.5.3.RELEASE</version>
    </dependency>

I did not find a complete example which can be used for this requirement.I came up with this configuration after checking various websites and selecting the ones I need for this setup. Please advise.

NOTE :- I have already implemented posting to the exchange and reading from the queue. However I need advise on how to implement failure scenarios. Please advise.

user3504187
  • 83
  • 3
  • 12

2 Answers2

0

Could you please post the entire stacktrace ? What happens if you use an old fashioned

<bean id="amqpTemplate" class="org.springframework.amqp.core.AmqpTemplate" >
</bean
JVXR
  • 1,294
  • 1
  • 11
  • 20
0

This is the simplest success scenario and I suggest you to follow the way that see here. First of all make sure you are connected to the right server and then add more things to your flow. I guess you missed vhost anyway take a look in below. (Also you forgot to mention what issue you are facing)

<int-amqp:inbound-channel-adapter channel="fromRabbit" queue-names="${rabbitmq.sourcequeue}" connection-factory="connectionFactory" concurrent-consumers="5" />
<int:channel id="fromRabbit">
        <int:interceptors>
            <int:wire-tap channel="consoleOut" />
        </int:interceptors>
</int:channel>
<int-stream:stdout-channel-adapter id="consoleOut"  append-newline="true" />
<!-- Infrastructure -->
<rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}"  port="${rabbitmq.port}" username="${rabbitmq.username}" password="${rabbitmq.password}" virtual-host="${rabbitmq.vhost}" />
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" />
<rabbit:admin connection-factory="connectionFactory" />
<rabbit:queue name="${rabbitmq.sourcequeue}" />
<rabbit:direct-exchange name="${rabbitmq.exchangeaname}">
  <rabbit:bindings>
   <rabbit:binding queue="${rabbitmq.sourcequeue}" key="${rabbitmq.keyname}" />
  </rabbit:bindings>
</rabbit:direct-exchange>
Shahab A
  • 71
  • 1
  • 4