com.testingsyndicate.jms.responder.ResponderServer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jms-responder-core Show documentation
Show all versions of jms-responder-core Show documentation
A stub for JMS Request/Reply
package com.testingsyndicate.jms.responder;
import com.testingsyndicate.jms.responder.model.config.ConnectionFactoryConfig;
import com.testingsyndicate.jms.responder.model.config.FileConfig;
import com.testingsyndicate.jms.responder.repository.FixedResponseRepository;
import com.testingsyndicate.jms.responder.repository.ResponseRepository;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Session;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public final class ResponderServer implements AutoCloseable {
private static final Logger LOG = LoggerFactory.getLogger(ResponderServer.class);
private final ConnectionFactory connectionFactory;
private final List queueNames;
private final ResponseRepository repository;
private final ExecutorService executor;
private final List sessions;
private Connection connection;
private boolean started = false;
private ResponderServer(Builder builder) {
connectionFactory = builder.connectionFactory;
queueNames = builder.queueNames;
repository = builder.repository;
if (null == builder.executor) {
executor = Executors.newCachedThreadPool();
} else {
executor = builder.executor;
}
sessions = new ArrayList<>();
}
public void start() {
synchronized (this) {
if (started) {
throw new IllegalStateException("Already started, Instances of ResponderServer are not reusable");
}
started = true;
try {
connection = connectionFactory.createConnection();
connection.start();
for (String queueName : queueNames) {
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
sessions.add(session);
executor.execute(new MessageHandler(session, queueName, repository));
}
} catch (JMSException ex) {
throw new RuntimeException("Error on startup", ex);
}
}
}
@Override
public void close() throws Exception {
synchronized (this) {
executor.shutdownNow();
for (Session session : sessions) {
try {
session.close();
} catch (JMSException ex) {
// Nom nom nom
}
}
sessions.clear();
if (null != connection) {
connection.close();
}
connection = null;
}
}
public static ResponderServer fromConfig(FileConfig config) throws Exception {
ConnectionFactoryConfig cfc = config.getConnectionFactory();
Class clazz = Class.forName(cfc.getClazz());
LOG.info("Initializing {}", clazz);
List> types = new ArrayList<>();
List