Search in sources :

Example 1 with FedMsgPublisherProviderData

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

the class FedMsgMessagingWorker method sendMessage.

@Override
public SendResult sendMessage(Run<?, ?> build, TaskListener listener, ProviderData pdata) {
    FedMsgPublisherProviderData pd = (FedMsgPublisherProviderData) pdata;
    ZMQ.Context context = ZMQ.context(1);
    ZMQ.Socket sock = context.socket(ZMQ.PUB);
    sock.setLinger(0);
    log.fine("pub address: " + provider.getPubAddr());
    sock.connect(provider.getPubAddr());
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    String body = "";
    String msgId = "";
    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", Objects.requireNonNull(build.getResult()).toString());
        }
        FedmsgMessage fm = new FedmsgMessage(PluginUtils.getSubstitutedValue(getTopic(provider), build.getEnvironment(listener)), PluginUtils.getSubstitutedValue(pd.getMessageContent(), env));
        fm.setTimestamp(System.currentTimeMillis());
        // Use toString() instead of getBodyJson so that message ID is included and sent.
        body = fm.toJson();
        msgId = fm.getMsgId();
        if (!sock.sendMore(fm.getTopic()) && pd.isFailOnError()) {
            log.severe("Unhandled exception in perform: Failed to send message (topic)!");
            return new SendResult(false, msgId, body);
        }
        if (!sock.send(body) && pd.isFailOnError()) {
            log.severe("Unhandled exception in perform: Failed to send message (body)!");
            return new SendResult(false, msgId, body);
        }
        log.fine("JSON message body:\n" + body);
        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 {
        sock.close();
        context.term();
    }
    return new SendResult(true, msgId, body);
}
Also used : EnvVars(hudson.EnvVars) FedmsgMessage(com.redhat.jenkins.plugins.ci.messaging.data.FedmsgMessage) SendResult(com.redhat.jenkins.plugins.ci.messaging.data.SendResult) FedMsgPublisherProviderData(com.redhat.jenkins.plugins.ci.provider.data.FedMsgPublisherProviderData) IOException(java.io.IOException) ZMQ(org.zeromq.ZMQ)

Example 2 with FedMsgPublisherProviderData

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

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

FedMsgPublisherProviderData (com.redhat.jenkins.plugins.ci.provider.data.FedMsgPublisherProviderData)3 IOException (java.io.IOException)3 ActiveMQPublisherProviderData (com.redhat.jenkins.plugins.ci.provider.data.ActiveMQPublisherProviderData)2 RabbitMQPublisherProviderData (com.redhat.jenkins.plugins.ci.provider.data.RabbitMQPublisherProviderData)2 FedmsgMessage (com.redhat.jenkins.plugins.ci.messaging.data.FedmsgMessage)1 SendResult (com.redhat.jenkins.plugins.ci.messaging.data.SendResult)1 EnvVars (hudson.EnvVars)1 ZMQ (org.zeromq.ZMQ)1