Search in sources :

Example 6 with ActiveMQSubscriberProviderData

use of com.redhat.jenkins.plugins.ci.provider.data.ActiveMQSubscriberProviderData in project jms-messaging-plugin by jenkinsci.

the class ActiveMqMessagingWorker method receive.

@Override
public void receive(String jobname, ProviderData pdata) {
    ActiveMQSubscriberProviderData pd = (ActiveMQSubscriberProviderData) pdata;
    int timeoutInMs = (pd.getTimeout() != null ? pd.getTimeout() : ActiveMQSubscriberProviderData.DEFAULT_TIMEOUT_IN_MINUTES) * 60 * 1000;
    while (!subscribe(jobname, pd.getSelector()) && !Thread.currentThread().isInterrupted()) {
        try {
            int WAIT_SECONDS = 2;
            Thread.sleep(WAIT_SECONDS * 1000);
        } catch (InterruptedException e) {
            // We were interrupted while waiting to retry. We will
            // jump ship on the next iteration.
            // NB: The interrupt flag was cleared when
            // InterruptedException was thrown. We have to
            // re-install it to make sure we eventually leave this
            // thread.
            Thread.currentThread().interrupt();
        }
    }
    if (!Thread.currentThread().isInterrupted()) {
        try {
            // In milliseconds!
            Message m = subscriber.receive(timeoutInMs);
            if (m != null) {
                if (provider.verify(getMessageBody(m), pd.getChecks(), jobname)) {
                    process(jobname, m);
                }
            } else {
                log.info("No message received for the past " + timeoutInMs + " ms, unsubscribing job '" + jobname + "'.");
                unsubscribe(jobname);
            }
        } catch (JMSException e) {
            if (!Thread.currentThread().isInterrupted()) {
                // Something other than an interrupt causes this.
                // Unsubscribe, but stay in our loop and try to reconnect..
                log.log(Level.WARNING, "JMS exception raised while receiving, unsubscribing job '" + jobname + "'.", e);
                // Try again next time.
                unsubscribe(jobname);
            }
        }
    } else {
        // We are about to leave the loop, so unsubscribe.
        unsubscribe(jobname);
    }
}
Also used : ActiveMQSubscriberProviderData(com.redhat.jenkins.plugins.ci.provider.data.ActiveMQSubscriberProviderData) MapMessage(javax.jms.MapMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) BytesMessage(javax.jms.BytesMessage) JMSException(javax.jms.JMSException)

Example 7 with ActiveMQSubscriberProviderData

use of com.redhat.jenkins.plugins.ci.provider.data.ActiveMQSubscriberProviderData in project jms-messaging-plugin by jenkinsci.

the class ActiveMqMessagingWorker method waitForMessage.

@Override
public String waitForMessage(Run<?, ?> build, TaskListener listener, ProviderData pdata) {
    ActiveMQSubscriberProviderData pd = (ActiveMQSubscriberProviderData) pdata;
    String ip = null;
    try {
        ip = Inet4Address.getLocalHost().getHostAddress();
    } catch (UnknownHostException e) {
        log.severe("Unable to get localhost IP address.");
    }
    String ltopic = getTopic(provider);
    try {
        ltopic = PluginUtils.getSubstitutedValue(getTopic(provider), build.getEnvironment(listener));
    } catch (IOException | InterruptedException e) {
        log.warning(e.getMessage());
    }
    if (ip != null && provider.getAuthenticationMethod() != null && ltopic != null && provider.getBroker() != null) {
        log.info("Waiting for message with selector: " + pd.getSelector());
        listener.getLogger().println("Waiting for message with selector: " + pd.getSelector());
        Connection connection = null;
        MessageConsumer consumer = null;
        try {
            ActiveMQConnectionFactory connectionFactory = provider.getConnectionFactory();
            connection = connectionFactory.createConnection();
            connection.setClientID(ip + "_" + UUID.randomUUID());
            connection.start();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            if (provider.getUseQueues()) {
                Queue destination = session.createQueue(ltopic);
                consumer = session.createConsumer(destination, pd.getSelector(), false);
            } else {
                Topic destination = session.createTopic(ltopic);
                consumer = session.createDurableSubscriber(destination, jobname, pd.getSelector(), false);
            }
            long startTime = new Date().getTime();
            int timeout = (pd.getTimeout() != null ? pd.getTimeout() : ActiveMQSubscriberProviderData.DEFAULT_TIMEOUT_IN_MINUTES) * 60 * 1000;
            Message message;
            do {
                message = consumer.receive(timeout);
                if (message != null) {
                    String value = getMessageBody(message);
                    if (provider.verify(value, pd.getChecks(), jobname)) {
                        if (StringUtils.isNotEmpty(pd.getVariable())) {
                            EnvVars vars = new EnvVars();
                            vars.put(pd.getVariable(), value);
                            build.addAction(new CIEnvironmentContributingAction(vars));
                        }
                        log.info("Received message with selector: " + pd.getSelector() + "\n" + formatMessage(message));
                        listener.getLogger().println("Received message with selector: " + pd.getSelector() + "\n" + formatMessage(message));
                        return value;
                    }
                }
            } while ((new Date().getTime() - startTime) < timeout && message != null);
            log.info("Timed out waiting for message!");
            listener.getLogger().println("Timed out waiting for message!");
        } catch (Exception e) {
            log.log(Level.SEVERE, "Unhandled exception waiting for message.", e);
        } finally {
            if (consumer != null) {
                try {
                    consumer.close();
                } catch (Exception e) {
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (Exception e) {
                }
            }
        }
    } else {
        log.severe("One or more of the following is invalid (null): ip, user, password, topic, broker.");
    }
    return null;
}
Also used : MessageConsumer(javax.jms.MessageConsumer) UnknownHostException(java.net.UnknownHostException) MapMessage(javax.jms.MapMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) BytesMessage(javax.jms.BytesMessage) Connection(javax.jms.Connection) IOException(java.io.IOException) Date(java.util.Date) JMSException(javax.jms.JMSException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) InvalidSelectorException(javax.jms.InvalidSelectorException) JMSSecurityException(javax.jms.JMSSecurityException) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) ActiveMQSubscriberProviderData(com.redhat.jenkins.plugins.ci.provider.data.ActiveMQSubscriberProviderData) EnvVars(hudson.EnvVars) Topic(javax.jms.Topic) Queue(javax.jms.Queue) Session(javax.jms.Session) CIEnvironmentContributingAction(com.redhat.jenkins.plugins.ci.CIEnvironmentContributingAction)

Example 8 with ActiveMQSubscriberProviderData

use of com.redhat.jenkins.plugins.ci.provider.data.ActiveMQSubscriberProviderData in project jms-messaging-plugin by jenkinsci.

the class CIBuildTrigger method readResolve.

@Override
protected Object readResolve() {
    if (providers == null) {
        log.info("Migrating CIBuildTrigger for job '" + getJobName() + "'.");
        providers = new ArrayList<>();
        if (providerData == null) {
            if (providerName == null) {
                log.info("Provider is null for trigger for job '" + getJobName() + "'.");
                JMSMessagingProvider provider = GlobalCIConfiguration.get().getConfigs().get(0);
                if (provider != null) {
                    providerName = provider.getName();
                    providerUpdated = true;
                    saveJob();
                }
            }
            JMSMessagingProvider provider = GlobalCIConfiguration.get().getProvider(providerName);
            if (provider != null) {
                if (provider instanceof ActiveMqMessagingProvider) {
                    log.info("Creating '" + providerName + "' trigger provider data for job '" + getJobName() + "'.");
                    ActiveMQSubscriberProviderData a = new ActiveMQSubscriberProviderData(providerName);
                    a.setSelector(selector);
                    a.setOverrides(overrides);
                    a.setChecks(checks);
                    providers.add(a);
                    providerUpdated = true;
                    saveJob();
                } else if (provider instanceof FedMsgMessagingProvider) {
                    log.info("Creating '" + providerName + "' trigger provider data for job '" + getJobName() + "'.");
                    FedMsgSubscriberProviderData f = new FedMsgSubscriberProviderData(providerName);
                    f.setOverrides(overrides);
                    f.setChecks(checks);
                    providers.add(f);
                    providerUpdated = true;
                    saveJob();
                }
            } else {
                log.warning("Unable to find provider '" + providerName + "', so unable to upgrade job.");
            }
        } else {
            providers.add(providerData);
            providerUpdated = true;
            saveJob();
        }
    }
    return this;
}
Also used : FedMsgSubscriberProviderData(com.redhat.jenkins.plugins.ci.provider.data.FedMsgSubscriberProviderData) ActiveMqMessagingProvider(com.redhat.jenkins.plugins.ci.messaging.ActiveMqMessagingProvider) ActiveMQSubscriberProviderData(com.redhat.jenkins.plugins.ci.provider.data.ActiveMQSubscriberProviderData) JMSMessagingProvider(com.redhat.jenkins.plugins.ci.messaging.JMSMessagingProvider) FedMsgMessagingProvider(com.redhat.jenkins.plugins.ci.messaging.FedMsgMessagingProvider)

Aggregations

ActiveMQSubscriberProviderData (com.redhat.jenkins.plugins.ci.provider.data.ActiveMQSubscriberProviderData)8 FreeStyleProject (hudson.model.FreeStyleProject)4 Test (org.junit.Test)4 CIBuildTrigger (com.redhat.jenkins.plugins.ci.CIBuildTrigger)3 Shell (hudson.tasks.Shell)3 ActiveMqMessagingProvider (com.redhat.jenkins.plugins.ci.messaging.ActiveMqMessagingProvider)2 JMSMessagingProvider (com.redhat.jenkins.plugins.ci.messaging.JMSMessagingProvider)2 FedMsgSubscriberProviderData (com.redhat.jenkins.plugins.ci.provider.data.FedMsgSubscriberProviderData)2 IOException (java.io.IOException)2 BytesMessage (javax.jms.BytesMessage)2 JMSException (javax.jms.JMSException)2 MapMessage (javax.jms.MapMessage)2 Message (javax.jms.Message)2 TextMessage (javax.jms.TextMessage)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 CIEnvironmentContributingAction (com.redhat.jenkins.plugins.ci.CIEnvironmentContributingAction)1 CIMessageNotifier (com.redhat.jenkins.plugins.ci.CIMessageNotifier)1 FedMsgMessagingProvider (com.redhat.jenkins.plugins.ci.messaging.FedMsgMessagingProvider)1 ActiveMQPublisherProviderData (com.redhat.jenkins.plugins.ci.provider.data.ActiveMQPublisherProviderData)1 RabbitMQSubscriberProviderData (com.redhat.jenkins.plugins.ci.provider.data.RabbitMQSubscriberProviderData)1