0
votes

I am doing some exercises on JMS in Spring and I have some doubt.

Into the solution of an exercise I have this configuration class named JmsInfrastructureConfig:

package config;

import javax.jms.ConnectionFactory;
import javax.jms.Destination;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class JmsInfrastructureConfig {

    /**
     * Create a ConnectionFactory using ActiveMQ:
     */
    @Bean
    public ConnectionFactory connectionFactory(){
        return new ActiveMQConnectionFactory("vm://embedded?broker.persistent=false");
    }


    /**
     *  Create a Queue for Dining objects using ActiveMQ: 
     */
    @Bean
    public Destination diningQueue() {
        return new ActiveMQQueue("rewards.queue.dining");
    }


    /**
     *  Create a Queue for Confirmation objects using ActiveMQ: 
     */
    @Bean
    public Destination confirmationQueue() {
        return new ActiveMQQueue("rewards.queue.confirmation");
    }

}

I know that in JMS-based applications the Connection object is obtained from a ConnectionFactory.

From what I understand reading the documentation the Connection is one of the JMS core components and it is used to put messages into a queue\topic and to consume a messages from a queue\topic. Is this assertion true or this is a wrong assertion?

The JMS Connection is obtained from a factory object, so in the previous configuration class is declared this bean that create and return the specific factory:

@Bean
public ConnectionFactory connectionFactory(){
    return new ActiveMQConnectionFactory("vm://embedded?broker.persistent=false");
}

It is pretty clear for me but here I have my first doubt. It create a new ActiveMQConnectionFactory object because it is used Apache Active MQ as Message Oriented Middleware.

What exactly do Apache Active MQ (and a MOM in general)? It handle the queues and topics and the access to these objects?).

What exactly is vm://embedded?broker.persistent=false ? Is it the broker URL? how exactly have I to interpret this address?

Then into the JmsInfrastructureConfig class create these 2 beans:

// Create a Queue for Dining objects using ActiveMQ:
@Bean
public Destination diningQueue() {
    return new ActiveMQQueue("rewards.queue.dining");
}

// Create a Queue for Confirmation objects using ActiveMQ:
@Bean
public Destination confirmationQueue() {
    return new ActiveMQQueue("rewards.queue.confirmation");
}

Both creates a specific queue, named respectivelly: rewards.queue.dining and rewards.queue.confirmation (I think that these are the queues name, or not?)

So these beans create a queue where the producer can put a message and from what a consumer can read a message.

These methods return a Destination object, what exactly represent this object? Is it the gateway to access to the queue (where access means write on read from the queue)? Or what?

Another doubt is: why in the acrhitecture of this application the declaration of the bean that return a ConnectionFactory is in the same class where are definied the beans that creats queues?

1

1 Answers

2
votes

Is it used to put messages into a queue\topic and to consume?

Yes, it is.

What exactly is vm://embedded?broker.persistent=false

This is your brokerURL, and this is how you should interpret it:

  • vm - this is your protocol, meaning virtual machine (as you are using an embedded broker, running inside your own application)
  • embedded - this is your brokerName, so now you have an embedded broker called "embedded" (bit confusing)
  • ?broker.persistent=false - this is just a parameter of your broker. This is an optional parameter, and you could append more if you want to

I think that these are the queues name

Yes, these are your queues.

Destination

This is the object you can use to access your queue. By using it you can produce (send) or consume (read) messages.

why in the architecture of this application the declaration of the bean that return a ConnectionFactory is in the same class where are defined the beans that creats queues?

They do not have be necessarily in the same class. However that example aims to be as simple as it can, so it creates these queues for you on the spot, so you can use it.

What exactly do Apache Active MQ (and a MOM in general)? It handle the queues and topics and the access to these objects?).

I think it is way too broad a question to answer. If you need a very highlevel explanation, you should probably read something first, like this: Message broker