+ public ListeningClient init(String clientId) {
+ Connection connection = null;
+ try {
+ connection = jmsConnectionFactory.createConnection();
+ connection.setClientID(clientId);
+ connection.start();
+ ListeningClient client = new ListeningClient(connection);
+ return client;
+ } catch (JMSException e) {
+ throw new SlcException("Could not init listening client "
+ + clientId, e);
+ } finally {
+ }
+ }
+
+ public void close(String clientId) {
+ Session session = null;
+ ListeningClient client = getClient(clientId);
+ Connection connection = client.getConnection();
+ try {
+ session = client.getSession();
+ session.unsubscribe(clientId);
+ } catch (JMSException e) {
+ log.warn("Could not unsubscribe client " + clientId, e);
+ } finally {
+ JmsUtils.closeSession(session);
+ }
+
+ // JmsUtils.closeSession(client.getSession());
+ clients.remove(clientId);
+
+ try {
+ connection.stop();
+ connection.close();
+ } catch (JMSException e) {
+ throw new SlcException("Could not close JMS connection for client "
+ + clientId, e);
+ } finally {
+ clients.remove(clientId);
+ }
+ }
+
+ protected ListeningClient getClient(String clientId) {
+ ListeningClient client = clients.get(clientId);
+ if (client == null) {
+ // Lazy init
+ client = init(clientId);
+ clients.put(clientId, client);
+ }
+ return client;
+ }
+
+ protected class ListeningClient {
+ private final Connection connection;
+ private final Session session;
+
+ public ListeningClient(Connection connection) {
+ super();
+ this.connection = connection;
+ try {
+ session = connection.createSession(false,
+ Session.AUTO_ACKNOWLEDGE);
+ } catch (JMSException e) {
+ throw new SlcException("Cannot create session");
+ }
+ }
+
+ public Connection getConnection() {
+ return connection;
+ }
+
+ public Session getSession() {
+ return session;
+ }
+
+ }