Location>code7788 >text

solon integrated activemq-client (sdk)

Popularity:817 ℃/2024-12-23 21:33:37

The raw activemq-client sdk integration is very easy to use and more suitable for customization. Some students, however, may be unfamiliar with the raw interface and may want a concrete example.

<dependency>
    <groupId></groupId>
    <artifactId>activemq-client</artifactId>
    <version>${}</version>
</dependency>

<dependency>
    <groupId></groupId>
    <artifactId>activemq-pool</artifactId>
    <version>${}</version>
</dependency>

Students who wish to simplify their use even more:

activemq-solon-cloud-plugin (Easier to use, less customizable)

1. Add integration configuration

prior useSolon Initializer Make a Solon Web template project first and add the activemq-client dependency above. Then make a configuration convention (which can also be defined on demand):

  • "" as a configuration prefix
    • "properties" as public configuration
    • "producer" as an ecologist-only configuration (presumably not used)
    • "consumer" as a consumer-only configuration (presumably not used)

Specific configuration properties, referenced from: ActiveMQConnectionFactory

:
  name: "demo-app"
  group: "demo"

# Configuration can be freely defined to correspond to the @Bean code (see below for reference)
:
  properties: # Public Configuration (Configuration items, see: ActiveMQConnectionFactory) .
    brokerURL: "failover:tcp://localhost:61616"
    redeliveryPolicy: initialRedeliveryDelay
      initialRedeliveryDelay: 5000
      backOffMultiplier: 2
      useExponentialBackOff: true
      maximumRedeliveries: -1
      maximumRedeliveryDelay: 3600_000

Adding the java configurator

@Configuration
public class ActivemqConfig {
    @Bean(destroyMethod = "stop")
    public Connection client(@Inject("${}") Props common) throws Exception {
        String brokerURL = (String) ("brokerURL");
        String userName = (String) ("userName");
        String password = (String) ("password");

        ActiveMQConnectionFactory factory;
        if ((userName)) {
            factory = new ActiveMQConnectionFactory(brokerURL);
        } else {
            factory = new ActiveMQConnectionFactory(brokerURL, userName, password);
        }

        //Bind additional configurations and create connections
        Connection connection = (factory).createConnection();
        ();
        return connection;
    }

    @Bean
    public IProducer producer(Connection connection) throws Exception {
        return new IProducer(connection);
    }

    @Bean
    public void consumer(Connection connection,
                         MessageListener messageListener) throws Exception {
        Session session = (false, Session.CLIENT_ACKNOWLEDGE);

        Destination destination = ("");
        MessageConsumer consumer = (destination);

        (messageListener);
    }
}

activemq's message sending code is a bit more complex, so we can do a wrapper process (for the configuration build above), temporarily named IProducer:

public class IProducer {
    private Connection connection;

    public IProducer(Connection connection) {
         = connection;
    }

    public void send(String topic, MessageBuilder messageBuilder) throws JMSException {
        Session session = (false, Session.CLIENT_ACKNOWLEDGE);

        Destination destination = (topic);
        MessageProducer producer = (destination);

        (destination, (session));
    }

    @FunctionalInterface
    public static interface MessageBuilder {
        Message build(Session session) throws JMSException;
    }
}

3. Code application

sent (or produced), here the generation controller is requested by the user to then send the message (FYI):

@Controller
public class DemoController {
    @Inject
    private IProducer producer;

    @Mapping("/send")
    public void send(String msg) throws Exception {
        //dispatch
        ("", s -> ("test"));
    }
}

Listening (or consuming), the subscription callback is used here: (FYI)

@Component
public class DemoMessageListener implements MessageListener {
    @Override
    public void onMessage(Message message) {
        (message);
        (message::acknowledge);
    }
}