Search in sources :

Example 1 with UnknownMessageTypeException

use of cz.metacentrum.perun.engine.exceptions.UnknownMessageTypeException in project perun by CESNET.

the class MessageReceiver method run.

@Override
public void run() {
    while (running) {
        if (!queueAcquired) {
            try {
                log.debug("Creating new JMS queue " + queueName);
                // Step 1. Directly instantiate the JMS Queue object.
                queue = HornetQJMSClient.createQueue(queueName);
                // Step 9. Create a JMS Message Consumer
                log.debug("Creating JMS consumer");
                messageConsumer = session.createConsumer(queue);
                queueAcquired = true;
                log.debug("Ready to receive messages.");
                // messageConsumer.receive(timeout) is a blocking operation!
                waitTime = 0;
            } catch (InvalidDestinationException e) {
                queueAcquired = false;
                waitTime = setWaitTime(waitTime);
                log.error("Queue doesn't exist yet. We gonna wait a bit ({} s) and try it again.", (waitTime / 1000), e);
                // wait for a time mentioned in the error message before try it again
                try {
                    Thread.sleep(waitTime);
                } catch (InterruptedException interrupted) {
                    log.error(interrupted.toString(), interrupted);
                }
            } catch (JMSException e) {
                queueAcquired = false;
                waitTime = setWaitTime(waitTime);
                log.error("Something went wrong with JMS. We are gonna wait a bit ({} s) and try it again...", (waitTime / 1000), e);
            } catch (Exception e) {
                queueAcquired = false;
                waitTime = setWaitTime(waitTime);
                log.error("Can not continue. We gonna wait a bit ({} s) and try it again...", (waitTime / 1000), e);
            }
        } else {
            // Try to send queued messages first
            while (!inputMessages.isEmpty()) {
                TextMessage message = inputMessages.remove();
                try {
                    messageProducer.send(message, DeliveryMode.PERSISTENT, message.getIntProperty("priority"), 0);
                    log.trace("Message {} for dispatcher sent.\n", message.getText());
                } catch (JMSException e) {
                    queueAcquired = false;
                    log.error("Something went wrong with JMS. We are gonna restart and try it again...", e);
                    // goes back to reinitialize the connection
                    return;
                }
            }
            // Step 11. Receive the message
            TextMessage messageReceived = null;
            try {
                messageReceived = (TextMessage) messageConsumer.receive(timeout);
                if (messageReceived != null) {
                    final String message = messageReceived.getText();
                    String messageType = message.split("\\|", 2)[0].trim();
                    log.debug("RECEIVED MESSAGE:{}, Type:{}", message, messageType);
                    if (messageType.equalsIgnoreCase("task")) {
                        try {
                            taskExecutorMessageProcess.execute(new Runnable() {

                                @Override
                                public void run() {
                                    // TODO: Remove in future
                                    log.trace("I am going to call eventProcessor.receiveEvent(\"{}\") " + "in thread: {}", message, Thread.currentThread().getName());
                                    eventProcessor.receiveEvent(message);
                                }
                            });
                        } catch (TaskRejectedException ex) {
                            log.error("Task was rejected. Message {}", message);
                            throw ex;
                        }
                    } else if (messageType.equalsIgnoreCase("command")) {
                        // TODO: There is no need to put commandProcessor to
                        // a separate thread at the moment, however it is
                        // very likely to be so in a future.
                        commandProcessor.receiveCommand(message);
                    } else {
                        throw new UnknownMessageTypeException("UNKNOWN TYPE[" + messageType + "]");
                    }
                }
                waitTime = 0;
            } catch (InvalidDestinationException e) {
                queueAcquired = false;
                waitTime = setWaitTime(waitTime);
                log.error("Queue doesn't exist or the connection is broken. We gonna wait a bit (" + (waitTime / 1000) + "s) and try it again...", e);
            } catch (JMSException e) {
                queueAcquired = false;
                waitTime = setWaitTime(waitTime);
                log.error("Something went wrong with JMS. We gonna wait a bit (" + (waitTime / 1000) + "s) and try it again...", e);
            } catch (Exception e) {
                queueAcquired = false;
                waitTime = setWaitTime(waitTime);
                log.error("Can not continue. We gonna wait a bit (" + (waitTime / 1000) + "s) and try it again...", e);
            }
        }
        if (waitTime > 0) {
            if (waitTime > TOO_LONG) {
                // gonna be back after trying to reinitialize the connection
                return;
            }
            try {
                Thread.sleep(waitTime);
            } catch (InterruptedException e) {
                log.error(e.toString(), e);
            }
        }
    }
}
Also used : TaskRejectedException(org.springframework.core.task.TaskRejectedException) InvalidDestinationException(javax.jms.InvalidDestinationException) JMSException(javax.jms.JMSException) TaskRejectedException(org.springframework.core.task.TaskRejectedException) JMSException(javax.jms.JMSException) InvalidDestinationException(javax.jms.InvalidDestinationException) UnknownMessageTypeException(cz.metacentrum.perun.engine.exceptions.UnknownMessageTypeException) TextMessage(javax.jms.TextMessage) UnknownMessageTypeException(cz.metacentrum.perun.engine.exceptions.UnknownMessageTypeException)

Aggregations

UnknownMessageTypeException (cz.metacentrum.perun.engine.exceptions.UnknownMessageTypeException)1 InvalidDestinationException (javax.jms.InvalidDestinationException)1 JMSException (javax.jms.JMSException)1 TextMessage (javax.jms.TextMessage)1 TaskRejectedException (org.springframework.core.task.TaskRejectedException)1