Spring boot activemq overriding the connection factory - spring

I am new to Spring boot and i am trying to lookup my own connection factory instead of using the default 'ConnectionFactory' which Spring boot provides and also trying to lookup the already defined queue without using dynamicqueues.
How can i do that?
Should i add jndi.properties file and add it there so i can lookup?
Can someone suggest?

The Spring Integration configuration by default is looking for a
Spring Bean called ‘connectionFactory’.
Spring Boot by default,
creates the JMS connection factory using the name
‘jmsConnectionFactory’.
#Bean
public ConnectionFactory jmsConnectionFactory() {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost");
return connectionFactory;
}
https://github.com/spring-projects/spring-boot/blob/v1.5.9.RELEASE/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQConnectionFactoryConfiguration.java

Related

TypeMismatchNamingException in spring boot with IBM MQ

I am trying to create a Spring boot project to read messages from a queue and do some processing.
I have defined the Jndi ConnectionFactory in application.properties
spring.jms.jndi-name=java:/MyConnectionFactory
On starting the application I am getting the following exception:
Caused by: org.springframework.jndi.TypeMismatchNamingException: Object of type [class com.ibm.mq.connector.outbound.ConnectionFactoryImpl] available at JNDI location [java:/MyConnectionFactory] is not assignable to [javax.jms.ConnectionFactory]
I am deploying the code on a jboss server with the given jndi.
Not sure if in this scenario some different implementation is needed for the ConnectionFactory.
#Bean public DefaultMessageListenerContainer orderMessageListenerContainer() {
DefaultMessageListenerContainer endpoint = new DefaultMessageListenerContainer();
endpoint.setMessageListener(new YourMessageListener());
endpoint.setDestination("yourDestination");
endpoint.setConnectionFactory(connectionFactory());
return orderDefaultJmsListenerContainerFactory().createListenerContainer(endpoint);
}
Solved manually with DefaultMessageListenerContainer.

How does spring work when class is not a spring managed bean but an object referred inside is

I have a RestTemplate Bean in a spring application like this
#Bean
public RestTemplate createRT(HttpClient httpClient) {
return new RestTemplate(new HttpComponentsClientHttpRequestFactory(httpClient));
}
Note that HttpComponentsClientHttpRequestFactory isn't a spring managed bean.
HttpClient bean is created as follows:
#Bean
public CloseableHttpClient createHttpClient(PoolingHttpClientConnectionManager poolingConnectionManager, RequestConfig rc) throws NoSuchAlgorithmException {
CloseableHttpClient httpClient = HttpClientBuilder
.create()
.setConnectionManager(poolingConnectionManager)
.setDefaultRequestConfig(rc)
.build();
return httpClient;
}
Note that both PoolingHttpClientConnectionManager and RequestConfig are created as spring managed beans.
My question is that in my case, the http client factory object i.e. HttpComponentsClientHttpRequestFactory isn't a spring managed bean but all it's internal objects that it uses to create an HttpClient object for connections are spring managed beans.
So will the HttpClient object that this factory creates going to be a singleton and managed by Spring ? Also would this implementation be considered erroneous in any sense based on the outer object being jvm managed and all inner objects being spring managed ?
I am little confused with it.
There should be no problem with the given snippet with respect to beans lifecycle. You are right that bean HttpComponentsClientHttpRequestFactory will not be Spring managed, but it would still be singleton, because the it is created inside a bean RestTemplate which is singleton. Note that RestTemplate bean would be created with name createRT.
Now coming to the point if it's considered erroneous: Although this is suggested that you should leave the bean and its dependency to be managed by Spring container. But for simple dependencies like in this case, I have seen developers create bean dependencies this way all the time, and everything works just well.
So, if you can, you should make this as a separate bean and let it come as a dependency for bean RestTemplate.

how to add several activemq NetworkConnectors in spring boot with java config, not configued with XML file

usually, we add NetworkConnectors configuration in activemq.xml before we start the activemq service as below:
<networkConnectors>
<networkConnector uri="static:(tcp://localhost:62001)"/>
</networkConnectors>
but this time, i just used spring boot with activemq embeded. and i want to configure more networkConnectors danymiclly when the mq running. so i could not choose to add these in activemq.xml. but need to configure with java code in spring boot. i don't know how to implement this.
you define the broker bean and add what you want as you do in xml
#Bean
public BrokerService broker() throws Exception {
BrokerService broker = new BrokerService();
broker.addConnector("tcp://localhost:5671");
broker.addNetworkConnector("static:(tcp://localhost:62001)");
return broker;
}

Spring boot log4j2 database appender

I am trying to use log4j2 database appender in a Spring boot project. I don't need any other database use in my project. I tried creating a ConnectionFactory using commons-dbcp2 and commons-pool2 (used sample codes available ), but unfortunately, Spring boot is failing during Startup, stating that it's unable to access ConnectionFactory. Seems like log4j2 is initialized before the ConnectionFactory. Is there a simple way to integrate log4j2 database appender in Spring boot, or it's something not possible? I don't want to make it complicated, and might use a different toolset, if these frameworks are not supposed to collaborate in a simpler manner.
Code that I used for ConnectionFactory:
DriverManagerConnectionFactory connectionFactory = new DriverManagerConnectionFactory(databaseUrl, properties);
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
GenericObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<PoolableConnection>(
poolableConnectionFactory);
poolableConnectionFactory.setPool(connectionPool);
this.dataSource = new PoolingDataSource<PoolableConnection>(connectionPool);

set heartbeat property on rabbitmq autoconfig connectionfactory bean

How should I set the heartbeat property on a CachingConnectionFactory bean in rabbitmq spring?
This is in a cloud foundry environment. So the application will be using a service binding via manifest file and I don't have the broker host name.
In my SimpleMessageListenerContainer bean, I make use of the CachingConnectionFactory bean and I guess it's autowired by Spring.
I could do in there,
#Bean
SimpleMessageListenerContainer container(CachingConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
connectionFactory.setRequestedHeartbeat(60);
container.setConnectionFactory(connectionFactory);
...
}
since I am not creating a bean for CachingconnectionFactory, I don't have a place for assigning that property, this is the only place I see for it.
Is there any other way to assign this property on ConnectionFactory in auto-configured setting?
thanks
See the Spring Boot Properties Documentation.
spring.rabbitmq.requested-heartbeat= # Requested heartbeat timeout, in seconds; zero for none.

Resources