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?

Fritz Duchardt
  • 11,026
  • 4
  • 41
  • 60
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • As I understand your question number 2-receiver contains an embedded ActiveMQ server. Is this correct? – gil.fernandes Jan 29 '18 at 15:10
  • @gil.fernandes - you are correct – gstackoverflow Jan 29 '18 at 15:15
  • 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 ... – gil.fernandes Jan 29 '18 at 15:18
  • take a look at https://stackoverflow.com/questions/43399072/spring-boot-configure-multiple-activemq-instances/43401330#43401330 – Hassen Bennour Jan 29 '18 at 15:18
  • 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? – pvpkiran Jan 29 '18 at 15:50
  • @pvpkiran, your suggestion is correct – gstackoverflow Jan 29 '18 at 16:18
  • 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 possible – pvpkiran Jan 29 '18 at 16:18

2 Answers2

13

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>
Roee Gavirel
  • 18,955
  • 12
  • 67
  • 94
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • If I understand correct you have sender(runner), receiver(@JmsListener) and activeMq(broker()) within the single aplication – gstackoverflow Jan 30 '18 at 08:36
  • 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. – Gary Russell Jan 30 '18 at 14:24
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;
}