amqp.spring.camel.component.SpringAMQPComponent Maven / Gradle / Ivy
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
package amqp.spring.camel.component;
import org.apache.camel.CamelContext;
import org.apache.camel.Endpoint;
import org.apache.camel.impl.DefaultComponent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import java.util.HashMap;
import java.util.Map;
public class SpringAMQPComponent extends DefaultComponent {
private static transient final Logger LOG = LoggerFactory.getLogger(SpringAMQPComponent.class);
protected Map connectionFactory;
protected Map amqpTemplate;
protected Map amqpAdministration;
public static final String ROUTING_KEY_HEADER = "ROUTING_KEY";
public static final String DEFAULT_CONNECTION = "DefaultConnection";
public static final String CONNECTION_HEADER = "CONNECTION=";
public SpringAMQPComponent() {
this(new CachingConnectionFactory());
}
public SpringAMQPComponent(CamelContext context) {
super(context);
//Attempt to load a connection factory from the registry
if(this.connectionFactory == null) {
this.connectionFactory = context.getRegistry().findByTypeWithName(ConnectionFactory.class);
if(this.connectionFactory != null && !this.connectionFactory.isEmpty()) {
for(Map.Entry connection : this.connectionFactory.entrySet()){
LOG.info("Found AMQP ConnectionFactory in registry for {}", connection.getValue().getHost());
}
}
}
if(this.connectionFactory == null) {
LOG.error("Cannot find a connection factory!");
}
}
public SpringAMQPComponent(ConnectionFactory connectionFactory) {
this.connectionFactory = new HashMap();
this.connectionFactory.put(DEFAULT_CONNECTION, connectionFactory);
}
public SpringAMQPComponent(Map connectionFactories) {
this.connectionFactory = connectionFactories;
}
@Override
protected Endpoint createEndpoint(String uri, String remaining, Map parameters) throws Exception {
String connection = remaining.contains(CONNECTION_HEADER) ? remaining.substring(remaining.indexOf(CONNECTION_HEADER)+CONNECTION_HEADER.length())
: connectionFactory.keySet().iterator().next();
SpringAMQPEndpoint endpoint = new SpringAMQPEndpoint(this, uri, remaining,
getAmqpTemplate().get(connection), getAmqpAdministration().get(connection));
setProperties(endpoint, parameters);
return endpoint;
}
public Map getConnectionFactory() {
return connectionFactory;
}
@Deprecated
public void setConnectionFactory(ConnectionFactory connectionFactory) {
this.connectionFactory = new HashMap();
this.connectionFactory.put(DEFAULT_CONNECTION, connectionFactory);
}
public void setConnectionFactory(Map connectionFactory) {
this.connectionFactory = connectionFactory;
}
public Map getAmqpAdministration() {
if(this.amqpAdministration == null && getCamelContext() != null && getCamelContext().getRegistry() != null) {
//Attempt to load an administration connection from the registry
this.amqpAdministration = new HashMap();
Map adminMap = getCamelContext().getRegistry().findByTypeWithName(AmqpAdmin.class);
for(AmqpAdmin admin : adminMap.values()){
CachingConnectionFactory adminConnection = (CachingConnectionFactory)((RabbitAdmin)admin).getRabbitTemplate().getConnectionFactory();
for(Map.Entry connection : this.connectionFactory.entrySet()){
if(adminConnection.getHost().equals(connection.getValue().getHost()) && adminConnection.getPort() == (connection.getValue().getPort())){
this.amqpAdministration.put(connection.getKey(), admin);
break;
}
}
}
if(this.amqpAdministration != null && !this.amqpAdministration.isEmpty()){
LOG.info("Found AMQP Administrator in registry");
}
}
if(this.amqpAdministration == null || this.amqpAdministration.isEmpty()) {
//Attempt to construct an AMQP Adminstration instance
this.amqpAdministration = new HashMap();
this.amqpAdministration.put(DEFAULT_CONNECTION, new RabbitAdmin(this.connectionFactory.values().iterator().next()));
LOG.info("Created new AMQP Administration instance");
}
return this.amqpAdministration;
}
@Deprecated
public void setAmqpAdministration(AmqpAdmin amqpAdministration) {
this.amqpAdministration = new HashMap(1);
this.amqpAdministration.put(DEFAULT_CONNECTION, amqpAdministration);
}
public void setAmqpAdministration(Map amqpAdministration) {
this.amqpAdministration = amqpAdministration;
}
public Map getAmqpTemplate() {
if(this.amqpTemplate == null && getCamelContext() != null && getCamelContext().getRegistry() != null) {
//Attempt to load an AMQP template from the registry
this.amqpTemplate = new HashMap();
Map templateMap = getCamelContext().getRegistry().findByTypeWithName(AmqpTemplate.class);
for(AmqpTemplate template : templateMap.values()){
CachingConnectionFactory adminConnection = (CachingConnectionFactory)((RabbitTemplate) template).getConnectionFactory();
for(Map.Entry connection : this.connectionFactory.entrySet()){
if(adminConnection.getHost().equals(connection.getValue().getHost()) && adminConnection.getPort() == (connection.getValue().getPort())){
this.amqpTemplate.put(connection.getKey(), template);
break;
}
}
}
if(this.amqpTemplate != null && !this.amqpTemplate.isEmpty()){
LOG.info("Found AMQP Template in registry");
}
}
if(this.amqpTemplate == null || this.amqpTemplate.isEmpty()) {
//Attempt to construct an AMQP template
this.amqpTemplate = new HashMap();
this.amqpTemplate.put(DEFAULT_CONNECTION, new RabbitTemplate(this.connectionFactory.values().iterator().next()));
LOG.info("Created new AMQP Template");
}
return this.amqpTemplate;
}
@Deprecated
public void setAmqpTemplate(AmqpTemplate amqpTemplate) {
this.amqpTemplate = new HashMap(1);
this.amqpTemplate.put(DEFAULT_CONNECTION, amqpTemplate);
}
public void setAmqpTemplate(Map amqpTemplate) {
this.amqpTemplate = amqpTemplate;
}
public static Throwable findRootCause(Throwable t) {
if(t.getCause() == null) return t;
return findRootCause(t.getCause());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy