Java Code Examples for javax.jms.QueueSender

The following examples show how to use javax.jms.QueueSender. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Test
public void processAndReply() throws JMSException {
	MessagingMessageListenerAdapter listener = createDefaultInstance(String.class);
	String body = "echo text";
	String correlationId = "link-1234";
	Destination replyDestination = new Destination() {};

	TextMessage reply = mock(TextMessage.class);
	QueueSender queueSender = mock(QueueSender.class);
	Session session = mock(Session.class);
	given(session.createTextMessage(body)).willReturn(reply);
	given(session.createProducer(replyDestination)).willReturn(queueSender);

	listener.setDefaultResponseDestination(replyDestination);
	StubTextMessage inputMessage = createSimpleJmsTextMessage(body);
	inputMessage.setJMSCorrelationID(correlationId);
	listener.onMessage(inputMessage, session);
	assertDefaultListenerMethodInvocation();

	verify(reply).setJMSCorrelationID(correlationId);
	verify(queueSender).send(reply);
	verify(queueSender).close();
}
 
Example 2
@Test
public void processAndReply() throws JMSException {
	MessagingMessageListenerAdapter listener = createDefaultInstance(String.class);
	String body = "echo text";
	String correlationId = "link-1234";
	Destination replyDestination = new Destination() {};

	TextMessage reply = mock(TextMessage.class);
	QueueSender queueSender = mock(QueueSender.class);
	Session session = mock(Session.class);
	given(session.createTextMessage(body)).willReturn(reply);
	given(session.createProducer(replyDestination)).willReturn(queueSender);

	listener.setDefaultResponseDestination(replyDestination);
	StubTextMessage inputMessage = createSimpleJmsTextMessage(body);
	inputMessage.setJMSCorrelationID(correlationId);
	listener.onMessage(inputMessage, session);
	assertDefaultListenerMethodInvocation();

	verify(reply).setJMSCorrelationID(correlationId);
	verify(queueSender).send(reply);
	verify(queueSender).close();
}
 
Example 3
Source Project: blog   Source File: InitConfServer.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void onMessage(Message message) {
	try {
		TextMessage receiveMessage = (TextMessage) message;
		String keys = receiveMessage.getText();
		LOGGER.info("keys = " + keys);
		MapMessage returnMess = session.createMapMessage();
		returnMess.setStringProperty("/a2/m1", "zhaohui");
		returnMess.setStringProperty("/a3/m1/v2", "nanjing");
		returnMess.setStringProperty("/a3/m1/v2/t2", "zhaohui");

		QueueSender sender = session.createSender((Queue) message.getJMSReplyTo());
		sender.send(returnMess);
	} catch (Exception e) {
		LOGGER.error("onMessage error", e);
	}
}
 
Example 4
Source Project: pooled-jms   Source File: JmsPoolQueueSenderTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testGetQueue() throws JMSException {
    JmsPoolConnection connection = (JmsPoolConnection) cf.createQueueConnection();
    QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = session.createTemporaryQueue();
    QueueSender sender = session.createSender(queue);

    assertNotNull(sender.getQueue());
    assertSame(queue, sender.getQueue());

    sender.close();

    try {
        sender.getQueue();
        fail("Cannot read topic on closed sender");
    } catch (IllegalStateException ise) {}
}
 
Example 5
Source Project: pooled-jms   Source File: XAConnectionPoolTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test(timeout = 60000)
public void testSenderAndPublisherDest() throws Exception {
    JmsPoolXAConnectionFactory pcf = createXAPooledConnectionFactory();

    QueueConnection connection = pcf.createQueueConnection();
    QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    QueueSender sender = session.createSender(session.createQueue("AA"));
    assertNotNull(sender.getQueue().getQueueName());

    connection.close();

    TopicConnection topicConnection = pcf.createTopicConnection();
    TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
    TopicPublisher topicPublisher = topicSession.createPublisher(topicSession.createTopic("AA"));
    assertNotNull(topicPublisher.getTopic().getTopicName());

    topicConnection.close();
    pcf.stop();
}
 
Example 6
Source Project: qpid-broker-j   Source File: QueueSenderTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void anonymousSenderSendToUnknownQueue() throws Exception
{
    QueueConnection connection = ((QueueConnection) getConnectionBuilder().setSyncPublish(true).build());

    try
    {
        QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue invalidDestination = session.createQueue("unknown");

        try
        {
            QueueSender sender = session.createSender(null);
            sender.send(invalidDestination, session.createMessage());
            fail("Exception not thrown");
        }
        catch (InvalidDestinationException e)
        {
            //PASS
        }
    }
    finally
    {
        connection.close();
    }
}
 
Example 7
@Test
public void processAndReply() throws JMSException {
	MessagingMessageListenerAdapter listener = createDefaultInstance(String.class);
	String body = "echo text";
	String correlationId = "link-1234";
	Destination replyDestination = new Destination() {};

	TextMessage reply = mock(TextMessage.class);
	QueueSender queueSender = mock(QueueSender.class);
	Session session = mock(Session.class);
	given(session.createTextMessage(body)).willReturn(reply);
	given(session.createProducer(replyDestination)).willReturn(queueSender);

	listener.setDefaultResponseDestination(replyDestination);
	StubTextMessage inputMessage = createSimpleJmsTextMessage(body);
	inputMessage.setJMSCorrelationID(correlationId);
	listener.onMessage(inputMessage, session);
	assertDefaultListenerMethodInvocation();

	verify(reply).setJMSCorrelationID(correlationId);
	verify(queueSender).send(reply);
	verify(queueSender).close();
}
 
Example 8
private void processAndReplyWithSendTo(MessagingMessageListenerAdapter listener, boolean pubSubDomain) throws JMSException {
	String body = "echo text";
	String correlationId = "link-1234";
	Destination replyDestination = new Destination() {};

	DestinationResolver destinationResolver = mock(DestinationResolver.class);
	TextMessage reply = mock(TextMessage.class);
	QueueSender queueSender = mock(QueueSender.class);
	Session session = mock(Session.class);

	given(destinationResolver.resolveDestinationName(session, "replyDestination", pubSubDomain))
			.willReturn(replyDestination);
	given(session.createTextMessage(body)).willReturn(reply);
	given(session.createProducer(replyDestination)).willReturn(queueSender);

	listener.setDestinationResolver(destinationResolver);
	StubTextMessage inputMessage = createSimpleJmsTextMessage(body);
	inputMessage.setJMSCorrelationID(correlationId);
	listener.onMessage(inputMessage, session);

	verify(destinationResolver).resolveDestinationName(session, "replyDestination", pubSubDomain);
	verify(reply).setJMSCorrelationID(correlationId);
	verify(queueSender).send(reply);
	verify(queueSender).close();
}
 
Example 9
/**
 * Send a message to testInboundQueue queue
 *
 * @throws Exception
 */
private void sendMessage() throws Exception {
    InitialContext initialContext = JmsClientHelper.getActiveMqInitialContext();
    QueueConnectionFactory connectionFactory
            = (QueueConnectionFactory) initialContext.lookup(JmsClientHelper.QUEUE_CONNECTION_FACTORY);
    QueueConnection queueConnection = connectionFactory.createQueueConnection();
    QueueSession queueSession = queueConnection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
    QueueSender sender = queueSession.createSender(queueSession.createQueue(QUEUE_NAME));

    String message = "<?xml version='1.0' encoding='UTF-8'?>" +
            "    <ser:getQuote xmlns:ser=\"http://services.samples\" xmlns:xsd=\"http://services.samples/xsd\"> " +
            "      <ser:request>" +
            "        <xsd:symbol>IBM</xsd:symbol>" +
            "      </ser:request>" +
            "    </ser:getQuote>";
    try {
        TextMessage jmsMessage = queueSession.createTextMessage(message);
        jmsMessage.setJMSType("incorrecttype");
        sender.send(jmsMessage);
    } finally {
        queueConnection.close();
    }
}
 
Example 10
Source Project: activemq-artemis   Source File: ActiveMQRASession.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Create a queue sender
 *
 * @param queue The queue
 * @return The queue sender
 * @throws JMSException Thrown if an error occurs
 */
@Override
public QueueSender createSender(final Queue queue) throws JMSException {
   lock();
   try {
      QueueSession session = getQueueSessionInternal();

      if (ActiveMQRALogger.LOGGER.isTraceEnabled()) {
         ActiveMQRALogger.LOGGER.trace("createSender " + session + " queue=" + queue);
      }

      QueueSender result = session.createSender(queue);
      result = new ActiveMQRAQueueSender(result, this);

      if (ActiveMQRALogger.LOGGER.isTraceEnabled()) {
         ActiveMQRALogger.LOGGER.trace("createdSender " + session + " sender=" + result);
      }

      addProducer(result);

      return result;
   } finally {
      unlock();
   }
}
 
Example 11
Source Project: javamail   Source File: SmtpJmsTransportTest.java    License: Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    System.setProperty(Context.INITIAL_CONTEXT_FACTORY, TestContextFactory.class.getName());
    QueueConnectionFactory queueConnectionFactory = Mockito.mock(QueueConnectionFactory.class);
    Queue queue = Mockito.mock(Queue.class);
    Context context = Mockito.mock(Context.class);
    TestContextFactory.context = context;
    when(context.lookup(eq("jms/queueConnectionFactory"))).thenReturn(queueConnectionFactory);
    when(context.lookup(eq("jms/mailQueue"))).thenReturn(queue);
    queueSender = Mockito.mock(QueueSender.class);
    QueueConnection queueConnection = Mockito.mock(QueueConnection.class);
    when(queueConnectionFactory.createQueueConnection()).thenReturn(queueConnection);
    when(queueConnectionFactory.createQueueConnection(anyString(), anyString())).thenReturn(queueConnection);
    QueueSession queueSession = Mockito.mock(QueueSession.class);
    bytesMessage = Mockito.mock(BytesMessage.class);
    when(queueSession.createBytesMessage()).thenReturn(bytesMessage);
    when(queueConnection.createQueueSession(anyBoolean(), anyInt())).thenReturn(queueSession);
    when(queueSession.createSender(eq(queue))).thenReturn(queueSender);
    transport = new SmtpJmsTransport(Session.getDefaultInstance(new Properties()), new URLName("jms"));
    transportListener = Mockito.mock(TransportListener.class);
    transport.addTransportListener(transportListener);
}
 
Example 12
Source Project: qpid-jms   Source File: JmsQueueSenderTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateQueueSender() throws Exception {
    JmsConnectionFactory factory = new JmsConnectionFactory(getBrokerAmqpConnectionURI());
    QueueConnection connection = factory.createQueueConnection();
    assertNotNull(connection);

    QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    assertNotNull(session);
    Queue queue = session.createQueue(name.getMethodName());
    QueueSender sender = session.createSender(queue);
    assertNotNull(sender);

    QueueViewMBean proxy = getProxyToQueue(name.getMethodName());
    assertEquals(0, proxy.getQueueSize());
    connection.close();
}
 
Example 13
Source Project: iaf   Source File: IfsaFacade.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Intended for server-side reponse sending and implies that the received
 * message *always* contains a reply-to address.
 */
public void sendReply(QueueSession session, Message received_message, String response) throws IfsaException {
	QueueSender tqs=null;
    try {
        TextMessage answer = session.createTextMessage();
        answer.setText(response);
		Queue replyQueue = (Queue)received_message.getJMSReplyTo();
        tqs = session.createSender(replyQueue );
        if (log.isDebugEnabled()) log.debug(getLogPrefix()+ "sending reply to ["+ received_message.getJMSReplyTo()+ "]");
        ((IFSAServerQueueSender) tqs).sendReply(received_message, answer);
    } catch (Throwable t) {
        throw new IfsaException(t);
    } finally {
    	if (tqs!=null) {
			try {
				tqs.close();
			} catch (JMSException e) {
				log.warn(getLogPrefix()+ "exception closing reply queue sender",e);
			}	
    	}
    }
}
 
Example 14
Source Project: brave   Source File: TracingMessageProducer.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void send(Queue queue, Message message, int deliveryMode, int priority, long timeToLive)
  throws JMSException {
  checkQueueSender();
  QueueSender qs = (QueueSender) delegate;
  Span span = createAndStartProducerSpan(message, destination(message));
  SpanInScope ws = tracer.withSpanInScope(span);
  Throwable error = null;
  try {
    qs.send(queue, message, deliveryMode, priority, timeToLive);
  } catch (Throwable t) {
    propagateIfFatal(t);
    error = t;
    throw t;
  } finally {
    if (error != null) span.error(error);
    span.finish();
    ws.close();
  }
}
 
Example 15
Source Project: micro-integrator   Source File: MDDProducer.java    License: Apache License 2.0 5 votes vote down vote up
private void sendBytesMessage(String destName, byte[] buffer) throws Exception {

        InitialContext ic = getInitialContext();
        QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) ic.lookup("ConnectionFactory");
        QueueConnection connection = queueConnectionFactory.createQueueConnection();
        QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        BytesMessage bm = session.createBytesMessage();
        bm.writeBytes(buffer);
        QueueSender sender = session.createSender((Queue) ic.lookup(destName));
        sender.send(bm);
        sender.close();
        session.close();
        connection.close();
    }
 
Example 16
Source Project: spring-analysis-note   Source File: MessageListenerAdapterTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testWithResponsiveMessageDelegateWithDefaultDestination_SendsReturnTextMessageWhenSessionSupplied() throws Exception {
	Queue destination = mock(Queue.class);
	TextMessage sentTextMessage = mock(TextMessage.class);
	// correlation ID is queried when response is being created...
	given(sentTextMessage.getJMSCorrelationID()).willReturn(
			CORRELATION_ID);
	// Reply-To is queried when response is being created...
	given(sentTextMessage.getJMSReplyTo()).willReturn(null); // we want to fall back to the default...

	TextMessage responseTextMessage = mock(TextMessage.class);

	QueueSender queueSender = mock(QueueSender.class);
	Session session = mock(Session.class);
	given(session.createTextMessage(RESPONSE_TEXT)).willReturn(responseTextMessage);
	given(session.createProducer(destination)).willReturn(queueSender);

	ResponsiveMessageDelegate delegate = mock(ResponsiveMessageDelegate.class);
	given(delegate.handleMessage(sentTextMessage)).willReturn(RESPONSE_TEXT);

	MessageListenerAdapter adapter = new MessageListenerAdapter(delegate) {
		@Override
		protected Object extractMessage(Message message) {
			return message;
		}
	};
	adapter.setDefaultResponseDestination(destination);
	adapter.onMessage(sentTextMessage, session);

	verify(responseTextMessage).setJMSCorrelationID(CORRELATION_ID);
	verify(queueSender).send(responseTextMessage);
	verify(queueSender).close();
	verify(delegate).handleMessage(sentTextMessage);
}
 
Example 17
Source Project: spring-analysis-note   Source File: MessageListenerAdapterTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testWithResponsiveMessageDelegateWhenReturnTypeIsAJMSMessageAndNoMessageConverterIsSupplied() throws Exception {
	Queue destination = mock(Queue.class);
	final TextMessage sentTextMessage = mock(TextMessage.class);
	// correlation ID is queried when response is being created...
	given(sentTextMessage.getJMSCorrelationID()).willReturn(CORRELATION_ID);
	// Reply-To is queried when response is being created...
	given(sentTextMessage.getJMSReplyTo()).willReturn(destination);

	TextMessage responseMessage = mock(TextMessage.class);
	QueueSender queueSender = mock(QueueSender.class);

	Session session = mock(Session.class);
	given(session.createProducer(destination)).willReturn(queueSender);

	ResponsiveJmsTextMessageReturningMessageDelegate delegate = mock(ResponsiveJmsTextMessageReturningMessageDelegate.class);
	given(delegate.handleMessage(sentTextMessage)).willReturn(responseMessage);

	final MessageListenerAdapter adapter = new MessageListenerAdapter(delegate) {
		@Override
		protected Object extractMessage(Message message) {
			return message;
		}
	};
	adapter.setMessageConverter(null);
	adapter.onMessage(sentTextMessage, session);

	verify(responseMessage).setJMSCorrelationID(CORRELATION_ID);
	verify(queueSender).send(responseMessage);
	verify(queueSender).close();
}
 
Example 18
private void processAndReplyWithSendTo(MessagingMessageListenerAdapter listener,
		String replyDestinationName, boolean pubSubDomain,
		QosSettings replyQosSettings) throws JMSException {
	String body = "echo text";
	String correlationId = "link-1234";
	Destination replyDestination = new Destination() {};

	DestinationResolver destinationResolver = mock(DestinationResolver.class);
	TextMessage reply = mock(TextMessage.class);
	QueueSender queueSender = mock(QueueSender.class);
	Session session = mock(Session.class);

	given(destinationResolver.resolveDestinationName(session, replyDestinationName, pubSubDomain))
			.willReturn(replyDestination);
	given(session.createTextMessage(body)).willReturn(reply);
	given(session.createProducer(replyDestination)).willReturn(queueSender);

	listener.setDestinationResolver(destinationResolver);
	StubTextMessage inputMessage = createSimpleJmsTextMessage(body);
	inputMessage.setJMSCorrelationID(correlationId);
	listener.onMessage(inputMessage, session);

	verify(destinationResolver).resolveDestinationName(session, replyDestinationName, pubSubDomain);
	verify(reply).setJMSCorrelationID(correlationId);
	if (replyQosSettings != null) {
		verify(queueSender).send(reply, replyQosSettings.getDeliveryMode(),
				replyQosSettings.getPriority(), replyQosSettings.getTimeToLive());
	}
	else {
		verify(queueSender).send(reply);
	}
	verify(queueSender).close();
}
 
Example 19
@Test
public void testWithResponsiveMessageDelegateWithDefaultDestination_SendsReturnTextMessageWhenSessionSupplied() throws Exception {
	Queue destination = mock(Queue.class);
	TextMessage sentTextMessage = mock(TextMessage.class);
	// correlation ID is queried when response is being created...
	given(sentTextMessage.getJMSCorrelationID()).willReturn(
			CORRELATION_ID);
	// Reply-To is queried when response is being created...
	given(sentTextMessage.getJMSReplyTo()).willReturn(null); // we want to fall back to the default...

	TextMessage responseTextMessage = mock(TextMessage.class);

	QueueSender queueSender = mock(QueueSender.class);
	Session session = mock(Session.class);
	given(session.createTextMessage(RESPONSE_TEXT)).willReturn(responseTextMessage);
	given(session.createProducer(destination)).willReturn(queueSender);

	ResponsiveMessageDelegate delegate = mock(ResponsiveMessageDelegate.class);
	given(delegate.handleMessage(sentTextMessage)).willReturn(RESPONSE_TEXT);

	MessageListenerAdapter adapter = new MessageListenerAdapter(delegate) {
		@Override
		protected Object extractMessage(Message message) {
			return message;
		}
	};
	adapter.setDefaultResponseDestination(destination);
	adapter.onMessage(sentTextMessage, session);

	verify(responseTextMessage).setJMSCorrelationID(CORRELATION_ID);
	verify(queueSender).send(responseTextMessage);
	verify(queueSender).close();
	verify(delegate).handleMessage(sentTextMessage);
}
 
Example 20
@Test
public void testWithResponsiveMessageDelegateWhenReturnTypeIsAJMSMessageAndNoMessageConverterIsSupplied() throws Exception {
	Queue destination = mock(Queue.class);
	final TextMessage sentTextMessage = mock(TextMessage.class);
	// correlation ID is queried when response is being created...
	given(sentTextMessage.getJMSCorrelationID()).willReturn(CORRELATION_ID);
	// Reply-To is queried when response is being created...
	given(sentTextMessage.getJMSReplyTo()).willReturn(destination);

	TextMessage responseMessage = mock(TextMessage.class);
	QueueSender queueSender = mock(QueueSender.class);

	Session session = mock(Session.class);
	given(session.createProducer(destination)).willReturn(queueSender);

	ResponsiveJmsTextMessageReturningMessageDelegate delegate = mock(ResponsiveJmsTextMessageReturningMessageDelegate.class);
	given(delegate.handleMessage(sentTextMessage)).willReturn(responseMessage);

	final MessageListenerAdapter adapter = new MessageListenerAdapter(delegate) {
		@Override
		protected Object extractMessage(Message message) {
			return message;
		}
	};
	adapter.setMessageConverter(null);
	adapter.onMessage(sentTextMessage, session);

	verify(responseMessage).setJMSCorrelationID(CORRELATION_ID);
	verify(queueSender).send(responseMessage);
	verify(queueSender).close();
}
 
Example 21
private void processAndReplyWithSendTo(MessagingMessageListenerAdapter listener,
		String replyDestinationName, boolean pubSubDomain,
		QosSettings replyQosSettings) throws JMSException {
	String body = "echo text";
	String correlationId = "link-1234";
	Destination replyDestination = new Destination() {};

	DestinationResolver destinationResolver = mock(DestinationResolver.class);
	TextMessage reply = mock(TextMessage.class);
	QueueSender queueSender = mock(QueueSender.class);
	Session session = mock(Session.class);

	given(destinationResolver.resolveDestinationName(session, replyDestinationName, pubSubDomain))
			.willReturn(replyDestination);
	given(session.createTextMessage(body)).willReturn(reply);
	given(session.createProducer(replyDestination)).willReturn(queueSender);

	listener.setDestinationResolver(destinationResolver);
	StubTextMessage inputMessage = createSimpleJmsTextMessage(body);
	inputMessage.setJMSCorrelationID(correlationId);
	listener.onMessage(inputMessage, session);

	verify(destinationResolver).resolveDestinationName(session, replyDestinationName, pubSubDomain);
	verify(reply).setJMSCorrelationID(correlationId);
	if (replyQosSettings != null) {
		verify(queueSender).send(reply, replyQosSettings.getDeliveryMode(),
				replyQosSettings.getPriority(), replyQosSettings.getTimeToLive());
	}
	else {
		verify(queueSender).send(reply);
	}
	verify(queueSender).close();
}
 
Example 22
Source Project: pooled-jms   Source File: JmsPoolQueueSenderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testToString() throws JMSException {
    JmsPoolConnection connection = (JmsPoolConnection) cf.createQueueConnection();
    QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = session.createTemporaryQueue();
    QueueSender sender = session.createSender(queue);

    assertNotNull(sender.toString());
}
 
Example 23
Source Project: pooled-jms   Source File: JmsPoolQueueSenderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testSendToQueueFailsIfNotAnonymousPublisher() throws JMSException {
    JmsPoolConnection connection = (JmsPoolConnection) cf.createTopicConnection();
    QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = session.createTemporaryQueue();
    QueueSender sender = session.createSender(queue);

    try {
        sender.send(session.createTemporaryQueue(), session.createTextMessage());
        fail("Should not be able to send to alternate destination");
    } catch (UnsupportedOperationException ex) {}
}
 
Example 24
Source Project: qpid-broker-j   Source File: QueueSenderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void sendToUnknownQueue() throws Exception
{
    QueueConnection connection = ((QueueConnection) getConnectionBuilder().build());

    try
    {
        QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue invalidDestination = session.createQueue("unknown");

        try
        {
            QueueSender sender = session.createSender(invalidDestination);
            sender.send(session.createMessage());
            fail("Exception not thrown");
        }
        catch (InvalidDestinationException e)
        {
            //PASS
        }
    }
    finally
    {
        connection.close();
    }
}
 
Example 25
/**
 * Build a dynamic proxy that reflectively adapts to JMS 2.0 API methods, if necessary.
 * Otherwise simply return this CachedMessageProducer instance itself.
 */
public MessageProducer getProxyIfNecessary() {
	if (completionListenerClass != null) {
		return (MessageProducer) Proxy.newProxyInstance(CachedMessageProducer.class.getClassLoader(),
				new Class<?>[] {MessageProducer.class, QueueSender.class, TopicPublisher.class},
				new Jms2MessageProducerInvocationHandler());
	}
	else {
		return this;
	}
}
 
Example 26
@Test
public void testWithResponsiveMessageDelegateWithDefaultDestination_SendsReturnTextMessageWhenSessionSupplied() throws Exception {
	Queue destination = mock(Queue.class);
	TextMessage sentTextMessage = mock(TextMessage.class);
	// correlation ID is queried when response is being created...
	given(sentTextMessage.getJMSCorrelationID()).willReturn(
			CORRELATION_ID);
	// Reply-To is queried when response is being created...
	given(sentTextMessage.getJMSReplyTo()).willReturn(null); // we want to fall back to the default...

	TextMessage responseTextMessage = mock(TextMessage.class);

	QueueSender queueSender = mock(QueueSender.class);
	Session session = mock(Session.class);
	given(session.createTextMessage(RESPONSE_TEXT)).willReturn(responseTextMessage);
	given(session.createProducer(destination)).willReturn(queueSender);

	ResponsiveMessageDelegate delegate = mock(ResponsiveMessageDelegate.class);
	given(delegate.handleMessage(sentTextMessage)).willReturn(RESPONSE_TEXT);

	MessageListenerAdapter adapter = new MessageListenerAdapter(delegate) {
		@Override
		protected Object extractMessage(Message message) {
			return message;
		}
	};
	adapter.setDefaultResponseDestination(destination);
	adapter.onMessage(sentTextMessage, session);

	verify(responseTextMessage).setJMSCorrelationID(CORRELATION_ID);
	verify(queueSender).send(responseTextMessage);
	verify(queueSender).close();
	verify(delegate).handleMessage(sentTextMessage);
}
 
Example 27
@Test
public void testWithResponsiveMessageDelegateWhenReturnTypeIsAJMSMessageAndNoMessageConverterIsSupplied() throws Exception {
	Queue destination = mock(Queue.class);
	final TextMessage sentTextMessage = mock(TextMessage.class);
	// correlation ID is queried when response is being created...
	given(sentTextMessage.getJMSCorrelationID()).willReturn(CORRELATION_ID);
	// Reply-To is queried when response is being created...
	given(sentTextMessage.getJMSReplyTo()).willReturn(destination);

	TextMessage responseMessage = mock(TextMessage.class);
	QueueSender queueSender = mock(QueueSender.class);

	Session session = mock(Session.class);
	given(session.createProducer(destination)).willReturn(queueSender);

	ResponsiveJmsTextMessageReturningMessageDelegate delegate = mock(ResponsiveJmsTextMessageReturningMessageDelegate.class);
	given(delegate.handleMessage(sentTextMessage)).willReturn(responseMessage);

	final MessageListenerAdapter adapter = new MessageListenerAdapter(delegate) {
		@Override
		protected Object extractMessage(Message message) {
			return message;
		}
	};
	adapter.setMessageConverter(null);
	adapter.onMessage(sentTextMessage, session);

	verify(responseMessage).setJMSCorrelationID(CORRELATION_ID);
	verify(queueSender).send(responseMessage);
	verify(queueSender).close();
}
 
Example 28
Source Project: zipkin-reporter-java   Source File: ActiveMQSender.java    License: Apache License 2.0 5 votes vote down vote up
void send() throws IOException {
  try {
    ActiveMQConn conn = lazyInit.get();
    QueueSender sender = conn.sender;
    BytesMessage bytesMessage = conn.session.createBytesMessage();
    bytesMessage.writeBytes(message);
    sender.send(bytesMessage);
  } catch (JMSException e) {
    throw ioException("Unable to send message: ", e);
  }
}
 
Example 29
Source Project: activemq-artemis   Source File: SimpleOpenWireTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testTempQueueDelete() throws Exception {
   connection.start();
   QueueSession queueSession = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

   TemporaryQueue tempQueue = queueSession.createTemporaryQueue();

   ActiveMQConnection newConn = (ActiveMQConnection) factory.createConnection();
   try {
      QueueSession newQueueSession = newConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
      QueueSender queueSender = newQueueSession.createSender(tempQueue);

      Message msg = queueSession.createMessage();
      queueSender.send(msg);

      try {
         QueueReceiver consumer = newQueueSession.createReceiver(tempQueue);
         fail("should have gotten exception but got consumer: " + consumer);
      } catch (JMSException ex) {
         //correct
      }

      connection.close();

      try {
         Message newMsg = newQueueSession.createMessage();
         queueSender.send(newMsg);
      } catch (JMSException e) {
         //ok
      }

   } finally {
      newConn.close();
   }
}
 
Example 30
Source Project: activemq-artemis   Source File: QueueReceiverTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * com.sun.ts.tests.jms.ee.all.queueconn.QueueConnTest line 171
 */
@Test
public void testCreateReceiverWithMessageSelector() throws Exception {
   QueueConnection qc = null;

   try {
      qc = createQueueConnection();
      QueueSession qs = qc.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

      QueueReceiver qreceiver = qs.createReceiver(queue1, "targetMessage = TRUE");

      qc.start();

      TextMessage m = qs.createTextMessage();
      m.setText("one");
      m.setBooleanProperty("targetMessage", false);

      QueueSender qsender = qs.createSender(queue1);

      qsender.send(m);

      m.setText("two");
      m.setBooleanProperty("targetMessage", true);

      qsender.send(m);

      TextMessage rm = (TextMessage) qreceiver.receive(1000);

      ProxyAssertSupport.assertEquals("two", rm.getText());
   } finally {
      if (qc != null) {
         qc.close();
      }
      Thread.sleep(2000);
      removeAllMessages(queue1.getQueueName(), true);
      checkEmpty(queue1);
   }
}