示例#1
0
  @Override
  public MessageProducerResources doCreateProducerModel() throws Exception {
    MessageProducerResources answer;
    Connection conn = getConnectionResource().borrowConnection();
    try {
      TransactionCommitStrategy commitStrategy = null;
      if (isEndpointTransacted()) {
        commitStrategy =
            getCommitStrategy() == null
                ? new DefaultTransactionCommitStrategy()
                : getCommitStrategy();
      }
      Session session = conn.createSession(isEndpointTransacted(), getAcknowledgeMode());
      Destination destination =
          getEndpoint()
              .getDestinationCreationStrategy()
              .createDestination(session, getDestinationName(), isTopic());
      MessageProducer messageProducer =
          JmsObjectFactory.createMessageProducer(session, destination, isPersistent(), getTtl());

      answer = new MessageProducerResources(session, messageProducer, commitStrategy);

    } catch (Exception e) {
      log.error("Unable to create the MessageProducer", e);
      throw e;
    } finally {
      getConnectionResource().returnConnection(conn);
    }
    return answer;
  }
示例#2
0
    @Override
    public MessageConsumerResources makeObject() throws Exception {
      MessageConsumerResources answer;
      Connection conn = getConnectionResource().borrowConnection();
      try {
        Session session;
        if (isEndpointTransacted()) {
          session = conn.createSession(true, Session.SESSION_TRANSACTED);
        } else {
          session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
        }

        Destination replyToDestination;
        if (ObjectHelper.isEmpty(getNamedReplyTo())) {
          replyToDestination =
              getEndpoint()
                  .getDestinationCreationStrategy()
                  .createTemporaryDestination(session, isTopic());
        } else {
          replyToDestination =
              getEndpoint()
                  .getDestinationCreationStrategy()
                  .createDestination(session, getNamedReplyTo(), isTopic());
        }
        MessageConsumer messageConsumer =
            JmsObjectFactory.createMessageConsumer(
                session, replyToDestination, null, isTopic(), null, true);
        messageConsumer.setMessageListener(
            new MessageListener() {

              @Override
              public void onMessage(final Message message) {
                log.debug("Message Received in the Consumer Pool");
                log.debug("  Message : {}", message);
                try {
                  Exchanger<Object> exchanger = EXCHANGERS.get(message.getJMSCorrelationID());
                  exchanger.exchange(message, getResponseTimeOut(), TimeUnit.MILLISECONDS);
                } catch (Exception e) {
                  log.error("Unable to exchange message: {}", message, e);
                }
              }
            });
        answer = new MessageConsumerResources(session, messageConsumer, replyToDestination);
      } catch (Exception e) {
        log.error("Unable to create the MessageConsumerResource: " + e.getLocalizedMessage());
        throw new CamelException(e);
      } finally {
        getConnectionResource().returnConnection(conn);
      }
      return answer;
    }
  @Test
  public void testInOnlyQueueProducer() throws Exception {
    MessageConsumer mc = JmsObjectFactory.createQueueConsumer(getSession(), TEST_DESTINATION_NAME);
    assertNotNull(mc);
    final String expectedBody = "Hello World!";
    MockEndpoint mock = getMockEndpoint("mock:result");

    mock.expectedMessageCount(1);
    mock.expectedBodiesReceived(expectedBody);

    template.sendBody("direct:start", expectedBody);
    Message message = mc.receive(5000);
    assertNotNull(message);
    assertTrue(message instanceof TextMessage);

    TextMessage tm = (TextMessage) message;
    String text = tm.getText();
    assertNotNull(text);

    template.sendBody("direct:finish", text);

    mock.assertIsSatisfied();
    mc.close();
  }