private ConnectionFactory obtainTargetConnectionFactory(Expression expression, Object rootObject) { if (expression != null && getConnectionFactory() instanceof AbstractRoutingConnectionFactory) { AbstractRoutingConnectionFactory routingConnectionFactory = (AbstractRoutingConnectionFactory) getConnectionFactory(); Object lookupKey; if (rootObject != null) { lookupKey = this.sendConnectionFactorySelectorExpression.getValue(this.evaluationContext, rootObject); } else { lookupKey = this.sendConnectionFactorySelectorExpression.getValue(this.evaluationContext); } if (lookupKey != null) { ConnectionFactory connectionFactory = routingConnectionFactory.getTargetConnectionFactory(lookupKey); if (connectionFactory != null) { return connectionFactory; } else if (!routingConnectionFactory.isLenientFallback()) { throw new IllegalStateException("Cannot determine target ConnectionFactory for lookup key [" + lookupKey + "]"); } } } return getConnectionFactory(); }
private ConnectionFactory obtainTargetConnectionFactory(Expression expression, Object rootObject) { if (expression != null && getConnectionFactory() instanceof AbstractRoutingConnectionFactory) { AbstractRoutingConnectionFactory routingConnectionFactory = (AbstractRoutingConnectionFactory) getConnectionFactory(); Object lookupKey; if (rootObject != null) { lookupKey = this.sendConnectionFactorySelectorExpression.getValue(this.evaluationContext, rootObject); } else { lookupKey = this.sendConnectionFactorySelectorExpression.getValue(this.evaluationContext); } if (lookupKey != null) { ConnectionFactory connectionFactory = routingConnectionFactory.getTargetConnectionFactory(lookupKey); if (connectionFactory != null) { return connectionFactory; } else if (!routingConnectionFactory.isLenientFallback()) { throw new IllegalStateException("Cannot determine target ConnectionFactory for lookup key [" + lookupKey + "]"); } } } return getConnectionFactory(); }
@Test public void testGetAddAndRemoveOperationsForTargetConnectionFactories() { ConnectionFactory targetConnectionFactory = Mockito.mock(ConnectionFactory.class); AbstractRoutingConnectionFactory routingFactory = new AbstractRoutingConnectionFactory() { @Override protected Object determineCurrentLookupKey() { return null; } }; //Make sure map is initialized and doesn't contain lookup key "1" assertNull(routingFactory.getTargetConnectionFactory("1")); //Add one and make sure it's there routingFactory.addTargetConnectionFactory("1", targetConnectionFactory); assertEquals(targetConnectionFactory, routingFactory.getTargetConnectionFactory("1")); assertNull(routingFactory.getTargetConnectionFactory("2")); //Remove it and make sure it's gone ConnectionFactory removedConnectionFactory = routingFactory.removeTargetConnectionFactory("1"); assertEquals(targetConnectionFactory, removedConnectionFactory); assertNull(routingFactory.getTargetConnectionFactory("1")); }