org.springframework.jms.JmsException Java Examples

The following examples show how to use org.springframework.jms.JmsException. 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: SensorPublisher.java    From oneops with Apache License 2.0 6 votes vote down vote up
/**
 * Publish message.
 *
 * @param event the event
 * @throws JMSException the jMS exception
 */
public void publishMessage(final BasicEvent event) throws JMSException {
    
    if (System.currentTimeMillis() > lastFailureTimestamp) {
        publishedCounter.incrementAndGet();
        int shard = (int) (event.getManifestId() % poolsize);
        try {
            producers[shard].send(session -> {
                ObjectMessage message = session.createObjectMessage(event);
                message.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
                message.setLongProperty("ciId", event.getCiId());
                message.setLongProperty("manifestId", event.getManifestId());
                message.setStringProperty("source", event.getSource());
                if (logger.isDebugEnabled()) {
                    logger.debug("Published: ciId:" + event.getCiId() + "; source:" + event.getSource());
                }
                return message;
            });
            lastFailureTimestamp = -1;
        } catch (JmsException exception) {
            logger.warn("There was an error sending a message. Discarding messages for " + mqConnectionThreshold + " ms");
            lastFailureTimestamp = System.currentTimeMillis() + mqConnectionThreshold;
        }
    }
}
 
Example #2
Source File: JmsTemplate.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
@Nullable
public <T> T browseSelected(final Queue queue, @Nullable final String messageSelector, final BrowserCallback<T> action)
		throws JmsException {

	Assert.notNull(action, "Callback object must not be null");
	return execute(session -> {
		QueueBrowser browser = createBrowser(session, queue, messageSelector);
		try {
			return action.doInJms(session, browser);
		}
		finally {
			JmsUtils.closeQueueBrowser(browser);
		}
	}, true);
}
 
Example #3
Source File: AbstractJmsListeningContainer.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Initialize this container.
 * <p>Creates a JMS Connection, starts the {@link javax.jms.Connection}
 * (if {@link #setAutoStartup(boolean) "autoStartup"} hasn't been turned off),
 * and calls {@link #doInitialize()}.
 * @throws org.springframework.jms.JmsException if startup failed
 */
public void initialize() throws JmsException {
	try {
		synchronized (this.lifecycleMonitor) {
			this.active = true;
			this.lifecycleMonitor.notifyAll();
		}
		doInitialize();
	}
	catch (JMSException ex) {
		synchronized (this.sharedConnectionMonitor) {
			ConnectionFactoryUtils.releaseConnection(this.sharedConnection, getConnectionFactory(), this.autoStartup);
			this.sharedConnection = null;
		}
		throw convertJmsAccessException(ex);
	}
}
 
Example #4
Source File: JmsTemplate.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * A variant of {@link #execute(SessionCallback, boolean)} that explicitly
 * creates a non-transactional {@link Session}. The given {@link SessionCallback}
 * does not participate in an existing transaction.
 */
@Nullable
private <T> T executeLocal(SessionCallback<T> action, boolean startConnection) throws JmsException {
	Assert.notNull(action, "Callback object must not be null");
	Connection con = null;
	Session session = null;
	try {
		con = createConnection();
		session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
		if (startConnection) {
			con.start();
		}
		if (logger.isDebugEnabled()) {
			logger.debug("Executing callback on JMS Session: " + session);
		}
		return action.doInJms(session);
	}
	catch (JMSException ex) {
		throw convertJmsAccessException(ex);
	}
	finally {
		JmsUtils.closeSession(session);
		ConnectionFactoryUtils.releaseConnection(con, getConnectionFactory(), startConnection);
	}
}
 
Example #5
Source File: ActiveJmsSender.java    From c2mon with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Send the collection of updates using the ActiveMQ JmsTemplate.
 *
 * @param dataTagValueUpdate the values to send
 * @throws JmsException if there is a problem sending the values
 */
@Override
public final void processValues(final DataTagValueUpdate dataTagValueUpdate) {
    // If the sending action is Enabled
    if (this.isEnabled && !dataTagValueUpdate.getValues().isEmpty()) {
      try {
        publish(dataTagValueUpdate);
      } catch (JmsException e) {
        if (primaryBroker) {
          log.error("Error occured when sending dataTagValueUpdate to primary JMS broker - submitting for republication", e);
          republisher.publicationFailed(dataTagValueUpdate);
        } else {
          log.error("Error occured when sending dataTagValueUpdate to secondary JMS broker - data is lost!", e);
        }
      }
    } else if (!this.isEnabled){
      log.debug("DAQ in test mode; not sending the value to JMS");
    }
  
}
 
Example #6
Source File: JmsTemplate.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void convertAndSend(
		Destination destination, final Object message, final MessagePostProcessor postProcessor)
		throws JmsException {

	send(destination, new MessageCreator() {
		@Override
		public Message createMessage(Session session) throws JMSException {
			Message msg = getRequiredMessageConverter().toMessage(message, session);
			return postProcessor.postProcessMessage(msg);
		}
	});
}
 
Example #7
Source File: JmsTemplate.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void convertAndSend(Object message) throws JmsException {
	Destination defaultDestination = getDefaultDestination();
	if (defaultDestination != null) {
		convertAndSend(defaultDestination, message);
	}
	else {
		convertAndSend(getRequiredDefaultDestinationName(), message);
	}
}
 
Example #8
Source File: DefaultMessageListenerContainer.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Stop this listener container, invoking the specific callback
 * once all listener processing has actually stopped.
 * <p>Note: Further {@code stop(runnable)} calls (before processing
 * has actually stopped) will override the specified callback. Only the
 * latest specified callback will be invoked.
 * <p>If a subsequent {@link #start()} call restarts the listener container
 * before it has fully stopped, the callback will not get invoked at all.
 * @param callback the callback to invoke once listener processing
 * has fully stopped
 * @throws JmsException if stopping failed
 * @see #stop()
 */
@Override
public void stop(Runnable callback) throws JmsException {
	synchronized (this.lifecycleMonitor) {
		if (!isRunning() || this.stopCallback != null) {
			// Not started, already stopped, or previous stop attempt in progress
			// -> return immediately, no stop process to control anymore.
			callback.run();
			return;
		}
		this.stopCallback = callback;
	}
	stop();
}
 
Example #9
Source File: BatchedJmsTemplate.java    From jadira with Apache License 2.0 5 votes vote down vote up
/**
 * Receive a batch of up to batchSize for given destination name and convert each message in the batch. Other than batching this method is the same as {@link JmsTemplate#receiveAndConvert(String)}
 * @return A list of {@link Message}
 * @param destinationName The destination name
 * @param batchSize The batch size
 * @throws JmsException The {@link JmsException}
 */
public List<Object> receiveAndConvertBatch(String destinationName, int batchSize) throws JmsException {
    List<Message> messages = receiveBatch(destinationName, batchSize);
    List<Object> result = new ArrayList<Object>(messages.size());
    for (Message next : messages) {
        result.add(doConvertFromMessage(next));
    }
    return result;
}
 
Example #10
Source File: JmsTemplate.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void convertAndSend(
		String destinationName, final Object message, final MessagePostProcessor postProcessor)
	throws JmsException {

	send(destinationName, new MessageCreator() {
		@Override
		public Message createMessage(Session session) throws JMSException {
			Message msg = getRequiredMessageConverter().toMessage(message, session);
			return postProcessor.postProcessMessage(msg);
		}
	});
}
 
Example #11
Source File: JmsTemplate.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Execute the action specified by the given action object within a
 * JMS Session. Generalized version of {@code execute(SessionCallback)},
 * allowing the JMS Connection to be started on the fly.
 * <p>Use {@code execute(SessionCallback)} for the general case.
 * Starting the JMS Connection is just necessary for receiving messages,
 * which is preferably achieved through the {@code receive} methods.
 * @param action callback object that exposes the Session
 * @param startConnection whether to start the Connection
 * @return the result object from working with the Session
 * @throws JmsException if there is any problem
 * @see #execute(SessionCallback)
 * @see #receive
 */
@Nullable
public <T> T execute(SessionCallback<T> action, boolean startConnection) throws JmsException {
	Assert.notNull(action, "Callback object must not be null");
	Connection conToClose = null;
	Session sessionToClose = null;
	try {
		Session sessionToUse = ConnectionFactoryUtils.doGetTransactionalSession(
				obtainConnectionFactory(), this.transactionalResourceFactory, startConnection);
		if (sessionToUse == null) {
			conToClose = createConnection();
			sessionToClose = createSession(conToClose);
			if (startConnection) {
				conToClose.start();
			}
			sessionToUse = sessionToClose;
		}
		if (logger.isDebugEnabled()) {
			logger.debug("Executing callback on JMS Session: " + sessionToUse);
		}
		return action.doInJms(sessionToUse);
	}
	catch (JMSException ex) {
		throw convertJmsAccessException(ex);
	}
	finally {
		JmsUtils.closeSession(sessionToClose);
		ConnectionFactoryUtils.releaseConnection(conToClose, getConnectionFactory(), startConnection);
	}
}
 
Example #12
Source File: JmsTemplate.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
@Nullable
public <T> T execute(final @Nullable Destination destination, final ProducerCallback<T> action) throws JmsException {
	Assert.notNull(action, "Callback object must not be null");
	return execute(session -> {
		MessageProducer producer = createProducer(session, destination);
		try {
			return action.doInJms(session, producer);
		}
		finally {
			JmsUtils.closeMessageProducer(producer);
		}
	}, false);
}
 
Example #13
Source File: JmsTemplate.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void send(MessageCreator messageCreator) throws JmsException {
	Destination defaultDestination = getDefaultDestination();
	if (defaultDestination != null) {
		send(defaultDestination, messageCreator);
	}
	else {
		send(getRequiredDefaultDestinationName(), messageCreator);
	}
}
 
Example #14
Source File: JmsMessagingTemplate.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Nullable
protected Message<?> doReceive(String destinationName) {
	try {
		javax.jms.Message jmsMessage = obtainJmsTemplate().receive(destinationName);
		return convertJmsMessage(jmsMessage);
	}
	catch (JmsException ex) {
		throw convertJmsException(ex);
	}
}
 
Example #15
Source File: JmsTemplate.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void convertAndSend(Object message) throws JmsException {
	Destination defaultDestination = getDefaultDestination();
	if (defaultDestination != null) {
		convertAndSend(defaultDestination, message);
	}
	else {
		convertAndSend(getRequiredDefaultDestinationName(), message);
	}
}
 
Example #16
Source File: JmsMessagingTemplate.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
protected MessagingException convertJmsException(JmsException ex) {
	if (ex instanceof org.springframework.jms.support.destination.DestinationResolutionException ||
			ex instanceof InvalidDestinationException) {
		return new DestinationResolutionException(ex.getMessage(), ex);
	}
	if (ex instanceof org.springframework.jms.support.converter.MessageConversionException) {
		return new MessageConversionException(ex.getMessage(), ex);
	}
	// Fallback
	return new MessagingException(ex.getMessage(), ex);
}
 
Example #17
Source File: BatchedJmsTemplate.java    From jadira with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Message receiveSelected(final String destinationName, final String messageSelector) throws JmsException {
    return execute(new SessionCallback<Message>() {
        public Message doInJms(Session session) throws JMSException {
            Destination destination = resolveDestinationName(session, destinationName);
            return doSingleReceive(session, destination, messageSelector);
        }
    }, true);
}
 
Example #18
Source File: JmsTemplate.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void send(final Destination destination, final MessageCreator messageCreator) throws JmsException {
	execute(new SessionCallback<Object>() {
		@Override
		public Object doInJms(Session session) throws JMSException {
			doSend(session, destination, messageCreator);
			return null;
		}
	}, false);
}
 
Example #19
Source File: JmsTemplate.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
@Nullable
public <T> T browse(BrowserCallback<T> action) throws JmsException {
	Queue defaultQueue = getDefaultQueue();
	if (defaultQueue != null) {
		return browse(defaultQueue, action);
	}
	else {
		return browse(getRequiredDefaultDestinationName(), action);
	}
}
 
Example #20
Source File: JmsTemplate.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void convertAndSend(Destination destination, final Object message) throws JmsException {
	send(destination, new MessageCreator() {
		@Override
		public Message createMessage(Session session) throws JMSException {
			return getRequiredMessageConverter().toMessage(message, session);
		}
	});
}
 
Example #21
Source File: MessageListenerTestContainer.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void start() throws JmsException {
	if (!initializationInvoked) {
		throw new IllegalStateException("afterPropertiesSet should have been invoked before start on " + this);
	}
	if (startInvoked) {
		throw new IllegalStateException("Start already invoked on " + this);
	}
	startInvoked = true;
}
 
Example #22
Source File: JmsTemplate.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
@Nullable
public Message sendAndReceive(final String destinationName, final MessageCreator messageCreator) throws JmsException {
	return executeLocal(session -> {
		Destination destination = resolveDestinationName(session, destinationName);
		return doSendAndReceive(session, destination, messageCreator);
	}, true);
}
 
Example #23
Source File: AbstractJmsListeningContainer.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Stop this container.
 * @throws JmsException if stopping failed
 * @see #doStop
 */
@Override
public void stop() throws JmsException {
	try {
		doStop();
	}
	catch (JMSException ex) {
		throw convertJmsAccessException(ex);
	}
}
 
Example #24
Source File: JmsTemplate.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T browseSelected(String messageSelector, BrowserCallback<T> action) throws JmsException {
	Queue defaultQueue = getDefaultQueue();
	if (defaultQueue != null) {
		return browseSelected(defaultQueue, messageSelector, action);
	}
	else {
		return browseSelected(getRequiredDefaultDestinationName(), messageSelector, action);
	}
}
 
Example #25
Source File: InstanceManager.java    From oneops with Apache License 2.0 5 votes vote down vote up
private void closeSensorListener() {
	try {
		for (SensorListenerContainer listenerContainer : sensorListenerContainers) {
			listenerContainer.stop();
			listenerContainer.shutdown();
		}
	} catch (JmsException e) {
		logger.error("There was an exception i stopping the opsmq listeners ",e);
		throw e;
	}
}
 
Example #26
Source File: BatchedJmsTemplate.java    From jadira with Apache License 2.0 5 votes vote down vote up
/**
 * Receive a batch of up to batchSize for given destination name and message selector and convert each message in the batch. Other than batching this method is the same as
 * {@link JmsTemplate#receiveSelectedAndConvert(String, String)}
 * @return A list of {@link Message}
 * @param destinationName The destination name
 * @param messageSelector The Selector
 * @param batchSize The batch size
 * @throws JmsException The {@link JmsException}
 */
public List<Object> receiveSelectedAndConvertBatch(String destinationName, String messageSelector, int batchSize)
        throws JmsException {
    List<Message> messages = receiveSelectedBatch(destinationName, batchSize);
    List<Object> result = new ArrayList<Object>(messages.size());
    for (Message next : messages) {
        result.add(doConvertFromMessage(next));
    }
    return result;
}
 
Example #27
Source File: BatchedJmsTemplate.java    From jadira with Apache License 2.0 5 votes vote down vote up
/**
 * Receive a batch of up to batchSize for default destination and message selector and convert each message in the batch. Other than batching this method is the same as
 * {@link JmsTemplate#receiveSelectedAndConvert(String)}
 * @return A list of {@link Message}
 * @param messageSelector The Selector
 * @param batchSize The batch size
 * @throws JmsException The {@link JmsException}
 */
public List<Object> receiveSelectedAndConvertBatch(String messageSelector, int batchSize) throws JmsException {
    List<Message> messages = receiveSelectedBatch(messageSelector, batchSize);
    List<Object> result = new ArrayList<Object>(messages.size());
    for (Message next : messages) {
        result.add(doConvertFromMessage(next));
    }
    return result;
}
 
Example #28
Source File: JmsMessagingTemplate.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected Message<?> doReceive(Destination destination) {
	try {
		javax.jms.Message jmsMessage = this.jmsTemplate.receive(destination);
		return convertJmsMessage(jmsMessage);
	}
	catch (JmsException ex) {
		throw convertJmsException(ex);
	}
}
 
Example #29
Source File: JmsTemplate.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void send(MessageCreator messageCreator) throws JmsException {
	Destination defaultDestination = getDefaultDestination();
	if (defaultDestination != null) {
		send(defaultDestination, messageCreator);
	}
	else {
		send(getRequiredDefaultDestinationName(), messageCreator);
	}
}
 
Example #30
Source File: JmsTemplate.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
@Nullable
public Message sendAndReceive(final Destination destination, final MessageCreator messageCreator) throws JmsException {
	return executeLocal(session -> doSendAndReceive(session, destination, messageCreator), true);
}