Polishing
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,7 +19,6 @@ package org.springframework.jms.connection;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import javax.jms.Connection;
|
||||
import javax.jms.ConnectionFactory;
|
||||
@@ -58,11 +57,11 @@ public class JmsResourceHolder extends ResourceHolderSupport {
|
||||
|
||||
private boolean frozen = false;
|
||||
|
||||
private final List<Connection> connections = new LinkedList<>();
|
||||
private final LinkedList<Connection> connections = new LinkedList<>();
|
||||
|
||||
private final List<Session> sessions = new LinkedList<>();
|
||||
private final LinkedList<Session> sessions = new LinkedList<>();
|
||||
|
||||
private final Map<Connection, List<Session>> sessionsPerConnection = new HashMap<>();
|
||||
private final Map<Connection, LinkedList<Session>> sessionsPerConnection = new HashMap<>();
|
||||
|
||||
|
||||
/**
|
||||
@@ -117,10 +116,19 @@ public class JmsResourceHolder extends ResourceHolderSupport {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return whether this resource holder is frozen, i.e. does not
|
||||
* allow for adding further Connections and Sessions to it.
|
||||
* @see #addConnection
|
||||
* @see #addSession
|
||||
*/
|
||||
public final boolean isFrozen() {
|
||||
return this.frozen;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the given Connection to this resource holder.
|
||||
*/
|
||||
public final void addConnection(Connection connection) {
|
||||
Assert.isTrue(!this.frozen, "Cannot add Connection because JmsResourceHolder is frozen");
|
||||
Assert.notNull(connection, "Connection must not be null");
|
||||
@@ -129,54 +137,92 @@ public class JmsResourceHolder extends ResourceHolderSupport {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the given Session to this resource holder.
|
||||
*/
|
||||
public final void addSession(Session session) {
|
||||
addSession(session, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the given Session to this resource holder,
|
||||
* registered for a specific Connection.
|
||||
*/
|
||||
public final void addSession(Session session, @Nullable Connection connection) {
|
||||
Assert.isTrue(!this.frozen, "Cannot add Session because JmsResourceHolder is frozen");
|
||||
Assert.notNull(session, "Session must not be null");
|
||||
if (!this.sessions.contains(session)) {
|
||||
this.sessions.add(session);
|
||||
if (connection != null) {
|
||||
List<Session> sessions = this.sessionsPerConnection.computeIfAbsent(connection, k -> new LinkedList<>());
|
||||
LinkedList<Session> sessions =
|
||||
this.sessionsPerConnection.computeIfAbsent(connection, k -> new LinkedList<>());
|
||||
sessions.add(session);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the given Session is registered
|
||||
* with this resource holder.
|
||||
*/
|
||||
public boolean containsSession(Session session) {
|
||||
return this.sessions.contains(session);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return this resource holder's default Connection,
|
||||
* or {@code null} if none.
|
||||
*/
|
||||
@Nullable
|
||||
public Connection getConnection() {
|
||||
return (!this.connections.isEmpty() ? this.connections.get(0) : null);
|
||||
return this.connections.peek();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return this resource holder's Connection of the given type,
|
||||
* or {@code null} if none.
|
||||
*/
|
||||
@Nullable
|
||||
public Connection getConnection(Class<? extends Connection> connectionType) {
|
||||
public <C extends Connection> C getConnection(Class<C> connectionType) {
|
||||
return CollectionUtils.findValueOfType(this.connections, connectionType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return this resource holder's default Session,
|
||||
* or {@code null} if none.
|
||||
*/
|
||||
@Nullable
|
||||
public Session getSession() {
|
||||
return (!this.sessions.isEmpty() ? this.sessions.get(0) : null);
|
||||
return this.sessions.peek();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return this resource holder's Session of the given type,
|
||||
* or {@code null} if none.
|
||||
*/
|
||||
@Nullable
|
||||
public Session getSession(Class<? extends Session> sessionType) {
|
||||
public <S extends Session> S getSession(Class<S> sessionType) {
|
||||
return getSession(sessionType, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return this resource holder's Session of the given type
|
||||
* for the given connection, or {@code null} if none.
|
||||
*/
|
||||
@Nullable
|
||||
public Session getSession(Class<? extends Session> sessionType, @Nullable Connection connection) {
|
||||
List<Session> sessions = (connection != null ? this.sessionsPerConnection.get(connection) : this.sessions);
|
||||
public <S extends Session> S getSession(Class<S> sessionType, @Nullable Connection connection) {
|
||||
LinkedList<Session> sessions =
|
||||
(connection != null ? this.sessionsPerConnection.get(connection) : this.sessions);
|
||||
return CollectionUtils.findValueOfType(sessions, sessionType);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Commit all of this resource holder's Sessions.
|
||||
* @throws JMSException if thrown from a Session commit attempt
|
||||
* @see Session#commit()
|
||||
*/
|
||||
public void commitAll() throws JMSException {
|
||||
for (Session session : this.sessions) {
|
||||
try {
|
||||
@@ -218,6 +264,10 @@ public class JmsResourceHolder extends ResourceHolderSupport {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Close all of this resource holder's Sessions and clear its state.
|
||||
* @see Session#close()
|
||||
*/
|
||||
public void closeAll() {
|
||||
for (Session session : this.sessions) {
|
||||
try {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -128,7 +128,7 @@ public class JmsTransactionManager extends AbstractPlatformTransactionManager
|
||||
* Set the JMS ConnectionFactory that this instance should manage transactions for.
|
||||
*/
|
||||
public void setConnectionFactory(@Nullable ConnectionFactory cf) {
|
||||
if (cf != null && cf instanceof TransactionAwareConnectionFactoryProxy) {
|
||||
if (cf instanceof TransactionAwareConnectionFactoryProxy) {
|
||||
// If we got a TransactionAwareConnectionFactoryProxy, we need to perform transactions
|
||||
// for its underlying target ConnectionFactory, else JMS access code won't see
|
||||
// properly exposed transactions (i.e. transactions for the target ConnectionFactory).
|
||||
|
||||
Reference in New Issue
Block a user