Search in sources :

Example 1 with ActiveMQPublisherProviderData

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

the class ActiveMqMessagingWorker method sendMessage.

@Override
public SendResult sendMessage(Run<?, ?> build, TaskListener listener, ProviderData pdata) {
    ActiveMQPublisherProviderData pd = (ActiveMQPublisherProviderData) pdata;
    Connection connection = null;
    Session session = null;
    MessageProducer publisher = null;
    TextMessage message;
    String mesgId = "0";
    String mesgContent = "";
    try {
        String ltopic = PluginUtils.getSubstitutedValue(getTopic(provider), build.getEnvironment(listener));
        if (provider.getAuthenticationMethod() != null && ltopic != null && provider.getBroker() != null) {
            ActiveMQConnectionFactory connectionFactory = provider.getConnectionFactory();
            connection = connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createTopic(ltopic);
            publisher = session.createProducer(destination);
            message = session.createTextMessage("");
            message.setJMSType(JSON_TYPE);
            TreeMap<String, String> envVarParts = new TreeMap<>();
            message.setStringProperty("CI_NAME", build.getParent().getName());
            envVarParts.put("CI_NAME", build.getParent().getName());
            if (pd.getMessageType() != null) {
                message.setStringProperty("CI_TYPE", pd.getMessageType().getMessage());
                envVarParts.put("CI_TYPE", pd.getMessageType().getMessage());
            }
            if (!build.isBuilding()) {
                String ciStatus = (build.getResult() == Result.SUCCESS ? "passed" : "failed");
                message.setStringProperty("CI_STATUS", ciStatus);
                envVarParts.put("CI_STATUS", ciStatus);
                envVarParts.put("BUILD_STATUS", build.getResult().toString());
            }
            EnvVars baseEnvVars = build.getEnvironment(listener);
            EnvVars envVars = new EnvVars();
            envVars.putAll(baseEnvVars);
            envVars.putAll(envVarParts);
            if (!StringUtils.isEmpty(pd.getMessageProperties())) {
                OrderedProperties p = new OrderedProperties();
                p.load(new StringReader(PluginUtils.getSubstitutedValue(pd.getMessageProperties(), envVars)));
                @SuppressWarnings("unchecked") Enumeration<String> e = p.propertyNames();
                while (e.hasMoreElements()) {
                    String key = e.nextElement();
                    if (!key.toLowerCase().startsWith("jms") || !setMessageHeader(message, key, p.getProperty(key), session)) {
                        EnvVars envVars2 = new EnvVars();
                        envVars2.putAll(baseEnvVars);
                        envVars2.putAll(envVarParts);
                        // This allows us to use recently added key/vals
                        // to be substituted
                        String val = PluginUtils.getSubstitutedValue(p.getProperty(key), envVars2);
                        message.setStringProperty(key, val);
                        envVarParts.put(key, val);
                    }
                }
            }
            EnvVars envVars2 = new EnvVars();
            envVars2.putAll(baseEnvVars);
            envVars2.putAll(envVarParts);
            message.setText(PluginUtils.getSubstitutedValue(pd.getMessageContent(), envVars2));
            publisher.send(message);
            mesgId = message.getJMSMessageID();
            mesgContent = message.getText();
            String messageType = "unknown type";
            if (pd.getMessageType() != null) {
                messageType = pd.getMessageType().toString();
            }
            log.info("Sent " + messageType + " message for job '" + build.getParent().getName() + "' to topic '" + ltopic + "':\n" + formatMessage(message));
        } else {
            log.severe("One or more of the following is invalid (null): user, password, topic, broker.");
            return new SendResult(false, mesgId, mesgContent);
        }
    } catch (Exception e) {
        if (pd.isFailOnError()) {
            log.severe("Unhandled exception in perform: ");
            log.severe(ExceptionUtils.getStackTrace(e));
            listener.fatalError("Unhandled exception in perform: ");
            listener.fatalError(ExceptionUtils.getStackTrace(e));
            return new SendResult(false, mesgId, mesgContent);
        } else {
            log.warning("Unhandled exception in perform: ");
            log.warning(ExceptionUtils.getStackTrace(e));
            listener.error("Unhandled exception in perform: ");
            listener.error(ExceptionUtils.getStackTrace(e));
            return new SendResult(true, mesgId, mesgContent);
        }
    } finally {
        if (publisher != null) {
            try {
                publisher.close();
            } catch (JMSException e) {
            }
        }
        if (session != null) {
            try {
                session.close();
            } catch (JMSException e) {
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
            }
        }
    }
    return new SendResult(true, mesgId, mesgContent);
}
Also used : Destination(javax.jms.Destination) Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) TreeMap(java.util.TreeMap) 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) EnvVars(hudson.EnvVars) ActiveMQPublisherProviderData(com.redhat.jenkins.plugins.ci.provider.data.ActiveMQPublisherProviderData) SendResult(com.redhat.jenkins.plugins.ci.messaging.data.SendResult) StringReader(java.io.StringReader) OrderedProperties(com.redhat.utils.OrderedProperties) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 2 with ActiveMQPublisherProviderData

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

the class AmqMessagingPluginWithFailoverIntegrationTest method testSimpleCIEventTrigger.

@Test
public void testSimpleCIEventTrigger() throws Exception {
    ArrayList<FreeStyleProject> jobs = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        FreeStyleProject jobA = j.createFreeStyleProject("receiver" + i);
        jobA.getBuildersList().add(new Shell("echo CI_TYPE = $CI_TYPE"));
        jobA.addTrigger(new CIBuildTrigger(true, Collections.singletonList(new ActiveMQSubscriberProviderData(SharedMessagingPluginIntegrationTest.DEFAULT_PROVIDER_NAME, null, "CI_TYPE = 'code-quality-checks-done' and CI_STATUS = 'failed'", Collections.emptyList(), "CI_MESSAGE", 60))));
    }
    waitForNoAMQTaskThreads();
    long currentThreadCount = getCurrentAMQThreadCount();
    System.out.println("Current AMQ Thread Count: " + currentThreadCount);
    String previousThreads = printAMQThreads();
    System.out.println(previousThreads);
    FreeStyleProject jobB = j.createFreeStyleProject("sender");
    jobB.getPublishersList().add(new CIMessageNotifier(new ActiveMQPublisherProviderData(SharedMessagingPluginIntegrationTest.DEFAULT_PROVIDER_NAME, null, MessageUtils.MESSAGE_TYPE.CodeQualityChecksDone, "CI_STATUS = failed", null, true)));
    j.buildAndAssertSuccess(jobB);
    for (FreeStyleProject job : jobs) {
        FreeStyleBuild lastBuild = job.getLastBuild();
        j.assertBuildStatusSuccess(lastBuild);
        j.assertLogContains("echo CI_TYPE = code-quality-checks-done", lastBuild);
        lastBuild.delete();
    }
    // Now stop AMQ
    System.out.println("Stopping AMQ");
    stopAMQ();
    System.out.println("Waiting 60 secs");
    Thread.sleep(60000);
    // Check for unconnection AMQ threads
    System.out.println(printAMQThreads());
    ensureNoUnconnectedThreads();
    // Now startup
    System.out.println("Starting AMQ");
    startAMQ();
    System.out.println("Waiting 10 secs");
    Thread.sleep(10000);
    waitForNoAMQTaskThreads();
    System.out.println(printAMQThreads());
    ensureNoLeakingThreads(currentThreadCount, previousThreads);
    j.buildAndAssertSuccess(jobB);
    Thread.sleep(1000);
    for (FreeStyleProject job : jobs) {
        FreeStyleBuild lastBuild = job.getLastBuild();
        j.assertBuildStatusSuccess(lastBuild);
        j.assertLogContains("echo CI_TYPE = code-quality-checks-done", lastBuild);
        lastBuild.delete();
    }
    System.out.println(printAMQThreads());
    System.out.println("Waiting 10 secs");
    Thread.sleep(10000);
    waitForNoAMQTaskThreads();
}
Also used : Shell(hudson.tasks.Shell) ActiveMQSubscriberProviderData(com.redhat.jenkins.plugins.ci.provider.data.ActiveMQSubscriberProviderData) CIMessageNotifier(com.redhat.jenkins.plugins.ci.CIMessageNotifier) ActiveMQPublisherProviderData(com.redhat.jenkins.plugins.ci.provider.data.ActiveMQPublisherProviderData) ArrayList(java.util.ArrayList) CIBuildTrigger(com.redhat.jenkins.plugins.ci.CIBuildTrigger) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) FreeStyleBuild(hudson.model.FreeStyleBuild) FreeStyleProject(hudson.model.FreeStyleProject) Test(org.junit.Test)

Example 3 with ActiveMQPublisherProviderData

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

the class MessageProviderMigrator method updateCIMessageBuilder.

private static boolean updateCIMessageBuilder(AbstractProject<?, ?> p, CIMessageBuilder builder) {
    if (builder.getProviderData() == null) {
        if (builder.getProviderName() == null) {
            builder.setProviderName(GlobalCIConfiguration.get().getConfigs().get(0).getName());
        }
        JMSMessagingProvider prov = GlobalCIConfiguration.get().getProvider(builder.getProviderName());
        if (prov instanceof ActiveMqMessagingProvider) {
            ActiveMQPublisherProviderData apd = new ActiveMQPublisherProviderData(builder.getProviderName());
            apd.setOverrides(builder.getOverrides());
            apd.setMessageType(builder.getMessageType());
            apd.setMessageProperties(builder.getMessageProperties());
            apd.setMessageContent(builder.getMessageContent());
            apd.setFailOnError(builder.isFailOnError());
            builder.setProviderData(apd);
        } else if (prov instanceof FedMsgMessagingProvider) {
            FedMsgPublisherProviderData fpd = new FedMsgPublisherProviderData(builder.getProviderName());
            fpd.setOverrides(builder.getOverrides());
            fpd.setMessageContent(builder.getMessageContent());
            fpd.setFailOnError(builder.isFailOnError());
            builder.setProviderData(fpd);
        } else {
            RabbitMQPublisherProviderData rpd = new RabbitMQPublisherProviderData(builder.getProviderName());
            rpd.setOverrides(builder.getOverrides());
            rpd.setMessageContent(builder.getMessageContent());
            rpd.setFailOnError(builder.isFailOnError());
            builder.setProviderData(rpd);
        }
        try {
            p.save();
            return true;
        } catch (IOException e) {
            log.log(Level.WARNING, "Failed to save project", e);
        }
    }
    return false;
}
Also used : ActiveMQPublisherProviderData(com.redhat.jenkins.plugins.ci.provider.data.ActiveMQPublisherProviderData) IOException(java.io.IOException) RabbitMQPublisherProviderData(com.redhat.jenkins.plugins.ci.provider.data.RabbitMQPublisherProviderData) FedMsgPublisherProviderData(com.redhat.jenkins.plugins.ci.provider.data.FedMsgPublisherProviderData)

Example 4 with ActiveMQPublisherProviderData

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

the class MessageProviderMigrator method updateCIMessageNotifier.

private static boolean updateCIMessageNotifier(AbstractProject<?, ?> p, CIMessageNotifier builder) {
    if (builder.getProviderData() == null) {
        if (builder.getProviderName() == null) {
            builder.setProviderName(GlobalCIConfiguration.get().getConfigs().get(0).getName());
        }
        JMSMessagingProvider prov = GlobalCIConfiguration.get().getProvider(builder.getProviderName());
        if (prov instanceof ActiveMqMessagingProvider) {
            ActiveMQPublisherProviderData apd = new ActiveMQPublisherProviderData(builder.getProviderName());
            apd.setOverrides(builder.getOverrides());
            apd.setMessageType(builder.getMessageType());
            apd.setMessageProperties(builder.getMessageProperties());
            apd.setMessageContent(builder.getMessageContent());
            apd.setFailOnError(builder.isFailOnError());
            builder.setProviderData(apd);
        } else if (prov instanceof FedMsgMessagingProvider) {
            FedMsgPublisherProviderData fpd = new FedMsgPublisherProviderData(builder.getProviderName());
            fpd.setOverrides(builder.getOverrides());
            fpd.setMessageContent(builder.getMessageContent());
            fpd.setFailOnError(builder.isFailOnError());
            builder.setProviderData(fpd);
        } else {
            RabbitMQPublisherProviderData rpd = new RabbitMQPublisherProviderData(builder.getProviderName());
            rpd.setOverrides(builder.getOverrides());
            rpd.setMessageContent(builder.getMessageContent());
            rpd.setFailOnError(builder.isFailOnError());
            builder.setProviderData(rpd);
        }
        try {
            p.save();
            return true;
        } catch (IOException e) {
            log.log(Level.WARNING, "Failed to save project", e);
        }
    }
    return false;
}
Also used : ActiveMQPublisherProviderData(com.redhat.jenkins.plugins.ci.provider.data.ActiveMQPublisherProviderData) IOException(java.io.IOException) RabbitMQPublisherProviderData(com.redhat.jenkins.plugins.ci.provider.data.RabbitMQPublisherProviderData) FedMsgPublisherProviderData(com.redhat.jenkins.plugins.ci.provider.data.FedMsgPublisherProviderData)

Aggregations

ActiveMQPublisherProviderData (com.redhat.jenkins.plugins.ci.provider.data.ActiveMQPublisherProviderData)4 IOException (java.io.IOException)3 FedMsgPublisherProviderData (com.redhat.jenkins.plugins.ci.provider.data.FedMsgPublisherProviderData)2 RabbitMQPublisherProviderData (com.redhat.jenkins.plugins.ci.provider.data.RabbitMQPublisherProviderData)2 CIBuildTrigger (com.redhat.jenkins.plugins.ci.CIBuildTrigger)1 CIMessageNotifier (com.redhat.jenkins.plugins.ci.CIMessageNotifier)1 SendResult (com.redhat.jenkins.plugins.ci.messaging.data.SendResult)1 ActiveMQSubscriberProviderData (com.redhat.jenkins.plugins.ci.provider.data.ActiveMQSubscriberProviderData)1 OrderedProperties (com.redhat.utils.OrderedProperties)1 EnvVars (hudson.EnvVars)1 FreeStyleBuild (hudson.model.FreeStyleBuild)1 FreeStyleProject (hudson.model.FreeStyleProject)1 Shell (hudson.tasks.Shell)1 StringReader (java.io.StringReader)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 TreeMap (java.util.TreeMap)1 Connection (javax.jms.Connection)1 Destination (javax.jms.Destination)1 InvalidSelectorException (javax.jms.InvalidSelectorException)1