Search in sources :

Example 1 with RabbitMQPublisherProviderData

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

the class RabbitMQMessagingPluginIntegrationTest method testFedoraMessagingHeaders.

@Test
public void testFedoraMessagingHeaders() throws Exception {
    FreeStyleProject job = j.createFreeStyleProject();
    job.getPublishersList().add(new CIMessageNotifier(new RabbitMQPublisherProviderData("test", null, "", true, true, 20, "schema")));
    FreeStyleBuild lastBuild = j.buildAndAssertSuccess(job);
    j.assertLogContains("fedora_messaging_severity=20, fedora_messaging_schema=schema}", lastBuild);
    j.assertLogContains("{sent_at=", lastBuild);
}
Also used : CIMessageNotifier(com.redhat.jenkins.plugins.ci.CIMessageNotifier) FreeStyleBuild(hudson.model.FreeStyleBuild) FreeStyleProject(hudson.model.FreeStyleProject) RabbitMQPublisherProviderData(com.redhat.jenkins.plugins.ci.provider.data.RabbitMQPublisherProviderData) Test(org.junit.Test)

Example 2 with RabbitMQPublisherProviderData

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

the class RabbitMQMessagingWorker method sendMessage.

@Override
public SendResult sendMessage(Run<?, ?> build, TaskListener listener, ProviderData pdata) {
    RabbitMQPublisherProviderData pd = (RabbitMQPublisherProviderData) pdata;
    try {
        if (connection == null || !connection.isOpen()) {
            connect();
        }
        if (channel == null || !channel.isOpen()) {
            this.channel = connection.createChannel();
            log.info("Channel created.");
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    String body = "";
    String msgId = "";
    // Fedora messaging wire format support
    Map<String, Object> headers = new HashMap<>();
    if (pd.isFedoraMessaging()) {
        headers.put("fedora_messaging_severity", pd.getSeverity());
        headers.put("fedora_messaging_schema", pd.getSchema());
        headers.put("sent_at", ZonedDateTime.now().toString());
    }
    System.out.println(headers);
    try {
        EnvVars env = new EnvVars();
        env.putAll(build.getEnvironment(listener));
        env.put("CI_NAME", build.getParent().getName());
        if (!build.isBuilding()) {
            env.put("CI_STATUS", (build.getResult() == Result.SUCCESS ? "passed" : "failed"));
            env.put("BUILD_STATUS", build.getResult().toString());
        }
        RabbitMQMessage msg = new RabbitMQMessage(PluginUtils.getSubstitutedValue(getTopic(provider), build.getEnvironment(listener)), PluginUtils.getSubstitutedValue(pd.getMessageContent(), env));
        msg.setTimestamp(System.currentTimeMillis() / 1000L);
        body = msg.getBodyJson();
        msgId = msg.getMsgId();
        try {
            channel.exchangeDeclarePassive(exchangeName);
            channel.basicPublish(exchangeName, msg.getTopic(), new AMQP.BasicProperties.Builder().headers(headers).messageId(msgId).build(), body.getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            if (pd.isFailOnError()) {
                log.severe("Unhandled exception in perform: Failed to send message!");
                return new SendResult(false, msgId, body);
            }
        }
        log.fine("Message headers:\n" + headers);
        log.fine("JSON message:\n" + msg.toJson());
        listener.getLogger().println("Message id: " + msg.getMsgId());
        listener.getLogger().println("Message topic: " + msg.getTopic());
        listener.getLogger().println("Message headers:\n" + headers);
        listener.getLogger().println("JSON message body:\n" + body);
    } 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, msgId, body);
        } 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, msgId, body);
        }
    } finally {
        try {
            channel.close();
        } catch (Exception e) {
            log.warning("Unhandled exception when closing channel: ");
            log.warning(ExceptionUtils.getStackTrace(e));
            listener.getLogger().println("exception in finally");
        }
    }
    return new SendResult(true, msgId, body);
}
Also used : EnvVars(hudson.EnvVars) HashMap(java.util.HashMap) AMQP(com.rabbitmq.client.AMQP) SendResult(com.redhat.jenkins.plugins.ci.messaging.data.SendResult) IOException(java.io.IOException) RabbitMQPublisherProviderData(com.redhat.jenkins.plugins.ci.provider.data.RabbitMQPublisherProviderData) IOException(java.io.IOException) RabbitMQMessage(com.redhat.jenkins.plugins.ci.messaging.data.RabbitMQMessage)

Example 3 with RabbitMQPublisherProviderData

use of com.redhat.jenkins.plugins.ci.provider.data.RabbitMQPublisherProviderData 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 RabbitMQPublisherProviderData

use of com.redhat.jenkins.plugins.ci.provider.data.RabbitMQPublisherProviderData 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

RabbitMQPublisherProviderData (com.redhat.jenkins.plugins.ci.provider.data.RabbitMQPublisherProviderData)4 IOException (java.io.IOException)3 ActiveMQPublisherProviderData (com.redhat.jenkins.plugins.ci.provider.data.ActiveMQPublisherProviderData)2 FedMsgPublisherProviderData (com.redhat.jenkins.plugins.ci.provider.data.FedMsgPublisherProviderData)2 AMQP (com.rabbitmq.client.AMQP)1 CIMessageNotifier (com.redhat.jenkins.plugins.ci.CIMessageNotifier)1 RabbitMQMessage (com.redhat.jenkins.plugins.ci.messaging.data.RabbitMQMessage)1 SendResult (com.redhat.jenkins.plugins.ci.messaging.data.SendResult)1 EnvVars (hudson.EnvVars)1 FreeStyleBuild (hudson.model.FreeStyleBuild)1 FreeStyleProject (hudson.model.FreeStyleProject)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1