Results 1 to 7 of 7

Thread: Just a quick one

Threaded View

  1. #1
    Join Date
    Oct 2012
    Posts
    11

    Default Just a quick one

    Judging by the solution to my previous (very first) post - I can only expect that I am again doing something fundamentally wrong here.

    I am attempting to do a synchronous JMS call and everything works fine if sender and receiver are deployed in the same JVM (kind of pointless but bear with me), if I split them into two separate 'test stubs' each with its own main the sender times out, even though it creates temporary queue etc. The receiver receives the message and sends it back yet the sync sender times out .

    Looked in the log file (I am using Active MQ) found lines confirming that temp queue gets created etc.. yet it times out. As I said if deployed within the same context (as components adn not CLI apps this time) it works fine(?!)

    I am really at a loss here and would appreciate if anyone could offer any insights.

    Regards,

    M.

    key code included:

    // Client

    Code:
    public static void main(String[] args) {
    ApplicationContext mainContext = new ClassPathXmlApplicationContext(
    				CONFIG_LOCATIONS);
    JmsTestClient jmsTestClient = mainContext.getBean("jmsTestClient", JmsTestClient.class);
    		
    		jmsTestClient.runTest();
    }
    
    private void runTest() {
    testType = jmsTestSyncSender.send("sendResponse");
    		
    		System.out.println("Received: " + ((testType != null) ? testType.getName() : "NULL"));
    	}
    
    // SyncSender send method
    
    public TestType send(final String str) {
    
    		TestType testType = null;
    
    		try {
    
    			testType = (TestType) jmsTemplate.execute(new SessionCallback<TestType>() {
    
    						public TestType doInJms(Session session)
    								throws JMSException {
    							MessageProducer producer = session
    									.createProducer(destination);
    							TemporaryQueue tempQueue = session
    									.createTemporaryQueue();
    							TextMessage message = session
    									.createTextMessage(str);
    
    							message.setJMSReplyTo(tempQueue);
    							message.setJMSCorrelationID(String.valueOf(System
    									.nanoTime()));
    							producer.send(message);
    
    							MessageConsumer consumer = session
    									.createConsumer(tempQueue);
    
    							Message response = consumer.receive(10000);
    
    							JmsUtils.closeMessageProducer(producer);
    							JmsUtils.closeMessageConsumer(consumer);
    
    							TestType returnedType = null;
    
    							if (response != null
    									&& response instanceof ActiveMQObjectMessage) {
    
    								try {
    									ObjectInputStream in = new ObjectInputStream(
    											new ByteArrayInputStream(
    													((ActiveMQObjectMessage) response)
    															.getContent().data));
    									returnedType = (TestType) in.readObject();
    
    									System.out.println((returnedType != null) ? returnedType.getName() : "NULL");
    
    								} catch (IOException e) {
    
    									e.printStackTrace();
    								} catch (ClassNotFoundException cnfe) {
    									cnfe.printStackTrace();
    								}
    
    							} else {
    
    								if (response != null
    										&& !(response instanceof ActiveMQObjectMessage)) {
    									System.out.println("Unsupported response detected ["
    											+ response.toString()
    											+ "] "
    											+ " expected "
    											+ ActiveMQObjectMessage.class
    													.getName()
    											+ " returning null.");
    								} else {
    									System.out.println(String.valueOf(response)
    											+ " response received.");
    								}
    
    							}
    
    							return returnedType;
    						}
    
    					});
    
    		} catch (JmsException e) {
    			e.printStackTrace();
    		}
    
    		return testType;
    	}
    
    // SyncReceiver
    
    @Component
    public class JmsTestSyncReceiver {
    
    	public TestType receive(String str) {
    
    		TestType testType = null;
    		System.out.println("Received: " + str);
    		
    		if (str.equalsIgnoreCase("sendResponse")) {
    			testType = new TestType();
    			testType.setName("Mutombo!");
    		}
    		
    		return testType;
    	}
    }
    
    <!-- Receiver side setup -->
    
    <bean id="jmsTestSyncContainer"
    		class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    		<property name="connectionFactory" ref="jmsFactory" />
    		<property name="destination" ref="testSyncDestination" />
    		<property name="messageListener" ref="testSyncMessageListener" />
    	</bean>
    
    	<bean id="testSyncDestination" class="org.apache.activemq.command.ActiveMQQueue">
    		<constructor-arg value="${TEST_TYPE_SYNC_QUEUE}" />
    	</bean>
    
    	<bean id="testSyncMessageListener"
    		class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
    		<constructor-arg>
    			<bean
    				class="jms.test.JmsTestSyncReceiver" />
    		</constructor-arg>
    		<property name="defaultListenerMethod" value="receive" />
    	</bean>
    
    // Sender side setup
    
    	<bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
    		<constructor-arg value="${TEST_TYPE_SYNC_QUEUE}" />
    	</bean>
    	
    	<bean id="jmsTestSyncSender"
    		class="jms.test.client.JmsTestSyncSender">
    	</bean>
    	
    	<bean id="jmsTestClient"
    		class="jms.test.client.JmsTestClient" />
    Last edited by Gary Russell; Nov 5th, 2012 at 06:37 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •