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);
}
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();
}
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;
}
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;
}
Aggregations