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