Announcement Announcement Module
Collapse
No announcement yet.
Spring JMS - 2 or more Senders and 2 or more Receivers (Spring + ActiveMq) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring JMS - 2 or more Senders and 2 or more Receivers (Spring + ActiveMq)

    Can someone help me??

    i need to create a JMS application..with only 1 send and receive it's ok, but now i need to create two or more Senders and Receivers. something like this.


    Teste1Sender - Teste1Receiver
    Teste2Sender - Teste2Receiver
    Teste3Sender - Teste3Receiver

    folllow my config and app for one sender and one receiver.

    ApplicationContext.xml
    Code:
                        	
    	<amq:broker useJmx="false" persistent="false">
    		<amq:transportConnectors>
    			<amq:transportConnector uri="tcp://localhost:0" />
    		</amq:transportConnectors>
        </amq:broker>
        
    
      <bean id="defaultQueue" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg value="defaultQueue"/>
      </bean>
      
      <bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">
        <property name="brokerURL" value="vm://localhost"/>
      </bean>
      
      <bean id="listenerContainer" class="org.springframework.jms.listener.SimpleMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory"/>
        <property name="destination" ref="defaultQueue"/>
        <property name="messageListener">
         <bean class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
              <property name="delegate">
                   <bean class="br.com.dnasolution.integrabrasil.ws.jms.MessageListenerImpl"/>
              </property>
         </bean>
        </property>
      </bean>
      
      <!-- This one is to send test messages -->
      <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="connectionFactory"/>
        <property name="defaultDestination" ref="defaultQueue"/>
      </bean>
    Receiver
    Code:
    @Component
    public class MessageListenerImpl implements MessageListener{
    
    	@Override
    	public void onMessage(Message message) {
    		System.out.println("RECEIVER");
    		  TextMessage msg = (TextMessage) message;
    		  Calendar dataEntradaComparar = Calendar.getInstance();
    		  dataEntradaComparar.add(Calendar.MINUTE, 1);
    		  int x = 0;
    		  while(Calendar.getInstance().getTimeInMillis() < dataEntradaComparar.getTimeInMillis()){
    			  /*try
    			  {
    			   System.out.println("MESSAGE: " + msg.getText());
    			  }
    			  catch (JMSException e)
    			  {
    			   e.printStackTrace();
    			  }*/
    			  x++;
    		  }
    		  
    		  try
    		  {
    		   System.out.println("MESSAGE: " + msg.getText());
    		  }
    		  catch (JMSException e)
    		  {
    		   e.printStackTrace();
    		  }
    	}
    
    	
    }
    And Sender
    Code:
    @Component
    public class MessageProducer
    {
    
     @Autowired
     private JmsTemplate jmsTemplate;
     
     private String mensagem;
     public void sendMessages(String msg) throws JMSException
     {
    	 mensagem = msg;
      System.out.println("PRODUCER");
      MessageCreator messageCreator = new MessageCreator()
      {
       public Message createMessage(Session session) throws JMSException
       {
        TextMessage message = session.createTextMessage(mensagem);
        return message;
       }
      };
      jmsTemplate.send(messageCreator);
     }
    }
Working...
X