Send message to a JMS queue using Spring Boot - spring-boot

I am new to Spring JMS. My application is developed using Spring Boot and is deployed in JBoss EAP 7.2.0. I have a remote queue which is an Active MQ Artemis queue which is also embedded within JBoss EAP 7.2.0. Can anyone please suggest me how to send a message to the remote JMS queue using JmsTemplate of Spring Boot? Basically I am not getting how should I define the remote connectionFactory to connect to the remote queue.

Add the following to application properties as your application is deployed in application server
spring.jms.jndi-name=java:/<your connection factory name for artemis>
Add artemis dependency and let spring boot autoconfigure jmsTemplate
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-artemis</artifactId>
</dependency>
Autowire jmsTemplate and send message
#Component
public class MyMessageSender {
#Autowired
JmsTemplate jmsTemplate;
public void send(String msg){
jmsTemplate.convertAndSend("my.queue.name", msg);
}
}
Optionally you can configure message converters and send pojos as message and let spring take care of converting it to json. For example
#Bean // Serialize message content to json using TextMessage
public MessageConverter jacksonJmsMessageConverter() {
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
converter.setTargetType(MessageType.TEXT);
converter.setTypeIdPropertyName("_type");
return converter;
}

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 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 cloud stream kafka binder to create consumer with on demand configuration

I am using Spring boot 1.5.9.RELEASE and Spring cloud Edgware.RELEASE across the Microservices.
I've bound a consumer using #EnableBinding annotation. The annotation will do rest of the part for me to consume events.
Some requirements came up to configure the topic name and some other configuration properties manually for which I want to override some of the properties of a consumer defined in an application.properties at application boot time.
Is there any direct way to do such?
You can use an initialization bean, it can do the work:
#SpringBootApplication
public class SpringDataDemoApplication {
#Bean
InitializingBean populateDatabase() {
return () -> {
// doWhatYouWantHere...
};
}

Spring boot activemq overriding the connection factory

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

how to use ApplicationEventPublisher in spring integration with annotation?

I am new to spring integration and I have to do some event based processing ? can anyone tell me how to use ApplicationEventPublisher. sample will be very helpful.
For publishing Spring Application events Spring Integration provides ApplicationEventPublishingMessageHandler component. This is one-way, just send producer and should be configured together with the #ServiceActivator annotations:
#ServiceActivator(inputChannel = "sendEventChannel")
#Bean
public MessageHandler eventProducer() {
return new ApplicationEventPublishingMessageHandler();
}
Also see http://docs.spring.io/spring-integration/reference/html/applicationevent.html.

Resources