Java Code Examples for org.springframework.jms.core.JmsTemplate#setReceiveTimeout()

The following examples show how to use org.springframework.jms.core.JmsTemplate#setReceiveTimeout() . 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
Source File: AbstractJMSProcessor.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
/**
 * This method essentially performs initialization of this Processor by
 * obtaining an instance of the {@link ConnectionFactory} from the
 * {@link JMSConnectionFactoryProvider} (ControllerService) and performing a
 * series of {@link ConnectionFactory} adaptations which eventually results
 * in an instance of the {@link CachingConnectionFactory} used to construct
 * {@link JmsTemplate} used by this Processor.
 */
private void buildTargetResource(ProcessContext context) {
    if (this.targetResource == null) {
        JMSConnectionFactoryProviderDefinition cfProvider = context.getProperty(CF_SERVICE).asControllerService(JMSConnectionFactoryProviderDefinition.class);
        ConnectionFactory connectionFactory = cfProvider.getConnectionFactory();

        UserCredentialsConnectionFactoryAdapter cfCredentialsAdapter = new UserCredentialsConnectionFactoryAdapter();
        cfCredentialsAdapter.setTargetConnectionFactory(connectionFactory);
        cfCredentialsAdapter.setUsername(context.getProperty(USER).getValue());
        cfCredentialsAdapter.setPassword(context.getProperty(PASSWORD).getValue());

        this.cachingConnectionFactory = new CachingConnectionFactory(cfCredentialsAdapter);
        this.cachingConnectionFactory.setSessionCacheSize(Integer.parseInt(context.getProperty(SESSION_CACHE_SIZE).getValue()));

        JmsTemplate jmsTemplate = new JmsTemplate();
        jmsTemplate.setConnectionFactory(this.cachingConnectionFactory);
        jmsTemplate.setPubSubDomain(TOPIC.equals(context.getProperty(DESTINATION_TYPE).getValue()));

        // set of properties that may be good candidates for exposure via configuration
        jmsTemplate.setReceiveTimeout(1000);

        this.targetResource = this.finishBuildingTargetResource(jmsTemplate, context);
    }
}
 
Example 2
Source File: AbstractJMSProcessor.java    From solace-integration-guides with Apache License 2.0 6 votes vote down vote up
/**
 * This method essentially performs initialization of this Processor by
 * obtaining an instance of the {@link ConnectionFactory} from the
 * {@link JMSConnectionFactoryProvider} (ControllerService) and performing a
 * series of {@link ConnectionFactory} adaptations which eventually results
 * in an instance of the {@link CachingConnectionFactory} used to construct
 * {@link JmsTemplate} used by this Processor.
 */
private void buildTargetResource(ProcessContext context) {
    if (this.targetResource == null) {
        JMSConnectionFactoryProviderDefinition cfProvider = context.getProperty(CF_SERVICE).asControllerService(JMSConnectionFactoryProviderDefinition.class);
        ConnectionFactory connectionFactory = cfProvider.getConnectionFactory();

        UserCredentialsConnectionFactoryAdapter cfCredentialsAdapter = new UserCredentialsConnectionFactoryAdapter();
        cfCredentialsAdapter.setTargetConnectionFactory(connectionFactory);
        cfCredentialsAdapter.setUsername(context.getProperty(USER).getValue());
        cfCredentialsAdapter.setPassword(context.getProperty(PASSWORD).getValue());

        this.cachingConnectionFactory = new CachingConnectionFactory(cfCredentialsAdapter);
        this.cachingConnectionFactory.setSessionCacheSize(Integer.parseInt(context.getProperty(SESSION_CACHE_SIZE).getValue()));

        JmsTemplate jmsTemplate = new JmsTemplate();
        jmsTemplate.setConnectionFactory(this.cachingConnectionFactory);
        jmsTemplate.setPubSubDomain(TOPIC.equals(context.getProperty(DESTINATION_TYPE).getValue()));

        // set of properties that may be good candidates for exposure via configuration
        jmsTemplate.setReceiveTimeout(1000);

        this.targetResource = this.finishBuildingTargetResource(jmsTemplate, context);
    }
}
 
Example 3
Source File: TagValuePublisherTest.java    From c2mon with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Listens for 1s for updates on the tag topic.
 */
private Thread startListenerThread(final Tag tag) {
  // start listener in separate thread (to catch update to topic)
  Thread listenerThread = new Thread(new Runnable() {

    @Override
    public void run() {
      try {
        JmsTemplate template = new JmsTemplate(connectionFactory);
        template.setReceiveTimeout(1000);
        synchronized (updateLock) {
          Message message = template.receive(new ActiveMQTopic("c2mon.client.tag." + ((DataTag) tag).getProcessId()));
          update = TransferTagSerializer.fromJson(((TextMessage) message).getText(), TransferTagValueImpl.class);
        }
      } catch (Exception e) {
        synchronized (updateLock) {
          update = null;
        }
      }
    }
  });
  listenerThread.start();
  return listenerThread;
}
 
Example 4
Source File: TagValuePublisherTest.java    From c2mon with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Listens for 1s for updates on the tag topic.
 */
private Thread startListenerThreadForTransferTag(final Tag tag) {
  // start listener in separate thread (to catch update to topic)
  Thread listenerThread = new Thread(new Runnable() {

    @Override
    public void run() {
      try {
        JmsTemplate template = new JmsTemplate(connectionFactory);
        template.setReceiveTimeout(5000);
        synchronized (updateLock) {
          Message message = template.receive(new ActiveMQTopic("c2mon.client.tag." + ((DataTag) tag).getProcessId()));
          updateFromConfig = TransferTagSerializer.fromJson(((TextMessage) message).getText(), TransferTagImpl.class);
        }
      } catch (Exception e) {
        synchronized (updateLock) {
          updateFromConfig = null;
        }
      }
    }
  });
  listenerThread.start();
  return listenerThread;
}
 
Example 5
Source File: ConsumeJMSManualTest.java    From nifi with Apache License 2.0 6 votes vote down vote up
private void send(MessageCreator messageCreator) throws Exception {
    final String  destinationName = "TEST";

    ConnectionFactory activeMqConnectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
    final ConnectionFactory connectionFactory = new CachingConnectionFactory(activeMqConnectionFactory);

    JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
    jmsTemplate.setPubSubDomain(false);
    jmsTemplate.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
    jmsTemplate.setReceiveTimeout(10L);

    try {
        JMSPublisher sender = new JMSPublisher((CachingConnectionFactory) jmsTemplate.getConnectionFactory(), jmsTemplate, mock(ComponentLog.class));

        sender.jmsTemplate.send(destinationName, messageCreator);
    } finally {
        ((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
    }
}
 
Example 6
Source File: SenderConfig.java    From spring-jms with MIT License 5 votes vote down vote up
@Bean
public JmsTemplate orderJmsTemplate() {
  JmsTemplate jmsTemplate =
      new JmsTemplate(cachingConnectionFactory());
  jmsTemplate.setDefaultDestination(orderDestination());
  jmsTemplate.setReceiveTimeout(5000);

  return jmsTemplate;
}
 
Example 7
Source File: ConsumeJMS.java    From nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Will create an instance of {@link JMSConsumer}
 */
@Override
protected JMSConsumer finishBuildingJmsWorker(CachingConnectionFactory connectionFactory, JmsTemplate jmsTemplate, ProcessContext processContext) {
    int ackMode = processContext.getProperty(ACKNOWLEDGEMENT_MODE).asInteger();
    jmsTemplate.setSessionAcknowledgeMode(ackMode);

    long timeout = processContext.getProperty(TIMEOUT).evaluateAttributeExpressions().asTimePeriod(TimeUnit.MILLISECONDS);
    jmsTemplate.setReceiveTimeout(timeout);

    return new JMSConsumer(connectionFactory, jmsTemplate, this.getLogger());
}
 
Example 8
Source File: CommonTest.java    From nifi with Apache License 2.0 5 votes vote down vote up
static JmsTemplate buildJmsTemplateForDestination(boolean pubSub) {
    ConnectionFactory activeMqConnectionFactory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
    final ConnectionFactory connectionFactory = new CachingConnectionFactory(activeMqConnectionFactory);

    JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
    jmsTemplate.setPubSubDomain(pubSub);
    jmsTemplate.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
    jmsTemplate.setReceiveTimeout(10L);
    return jmsTemplate;
}
 
Example 9
Source File: SpringJmsTemplateOneWay.java    From hazelcastmq with Apache License 2.0 4 votes vote down vote up
/**
 * Constructs the example.
 */
public SpringJmsTemplateOneWay() throws InterruptedException {
  // Create a Hazelcast instance.
  Config config = new Config();
  HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance(config);

  try {
    // HazelcastMQ Instance
    HazelcastMQConfig mqConfig = new HazelcastMQConfig();
    mqConfig.setHazelcastInstance(hazelcast);

    HazelcastMQInstance mqInstance = HazelcastMQ
        .newHazelcastMQInstance(mqConfig);

    // HazelcastMQJms Instance
    HazelcastMQJmsConfig mqJmsConfig = new HazelcastMQJmsConfig();
    mqJmsConfig.setHazelcastMQInstance(mqInstance);

    HazelcastMQJmsConnectionFactory connectionFactory = new HazelcastMQJmsConnectionFactory(
        mqJmsConfig);

    // Setup the JMS Template
    JmsTemplate jmsOps = new JmsTemplate(connectionFactory);
    jmsOps.setReceiveTimeout(5000);

    // Send the message.
    jmsOps.convertAndSend("foo.bar", "Hello World");

    // Simulate something interesting happening before we get around to
    // consuming the message.
    Thread.sleep(1000);

    // Receive the message.
    String msg = (String) jmsOps.receiveAndConvert("foo.bar");

    log.info("Got message: " + msg);
  }
  finally {
    hazelcast.getLifecycleService().shutdown();
  }
}