Is it possible to connect to spring boot embedded ActiveMQ instance from another application(started in separate process)?
Asked Answered
P

2

10

I've read several examples about jms support in spring boot.

and usually sender, receiver and active-mq(actually it can be any other jms compatible message broker) locates within the same application.

I know that I can use stand alone active mq and use properties:

spring.activemq.broker-url=tcp://192.168.1.210:9876
spring.activemq.user=admin
spring.activemq.password=secret

But I want to have 2 applications:

1- sender (connects to jms from receiver embedded and sends messages there)
2-receiver (up application and embedded activemq)

Is it posiible?

Pyrimidine answered 29/1, 2018 at 15:5 Comment(7)
As I understand your question number 2-receiver contains an embedded ActiveMQ server. Is this correct?Short
@Short - you are correctPyrimidine
In theory there should be no problem in doing what you propose. After all ActiveMQ is written in Java and can run embedded in another application and should be able to behave in the same manner as simply running standalone. You can do the same thing with an http server ...Short
take a look at #43399572Ara
not sure of I understood your question. There are two applications, Sender and Reciever. Reciever has embedded activemq. And sender application should connect to this embedded activemq from Reciever and send message. Is this what you are asking?Portmanteau
@pvpkiran, your suggestion is correctPyrimidine
Not possible. If it were possible, then there is no meaning to embedded right. How can you connect to an embedded activemq from outside of application. If Sender and Reciever are in same application, then yes it is possiblePortmanteau
P
14

Just add a BrokerService bean to your application:

@SpringBootApplication
public class So48504265Application {

    public static void main(String[] args) {
        SpringApplication.run(So48504265Application.class, args);
    }

    @Bean
    public BrokerService broker() throws Exception {
        BrokerService broker = new BrokerService();
        broker.addConnector("tcp://localhost:61616");
        return broker;
    }

    @Bean
    public ApplicationRunner runner(JmsTemplate template) {
        return args -> template.convertAndSend("foo", "AMessage");
    }

    @JmsListener(destination = "foo")
    public void listen(String in) {
        System.out.println(in);
    }

}

and

spring.activemq.broker-url=tcp://localhost:61616

and add this to your pom

<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-kahadb-store</artifactId>
</dependency>
Perreault answered 29/1, 2018 at 15:24 Comment(2)
If I understand correct you have sender(runner), receiver(@JmsListener) and activeMq(broker()) within the single aplicationPyrimidine
That is correct in this example but, because the broker is listening on the tcp:// transport instead of the default vm://, other applications can connect to it.Perreault
I
1

Add to your config to access local or remotely

@Bean
public BrokerService broker() throws Exception {
    BrokerService broker = new BrokerService();
    broker.addConnector("tcp://0.0.0.0:61616");
    brokerService.setPersistent(false);
    return broker;
}
Insnare answered 26/11, 2020 at 19:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.