All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.birchframework.bridge.BaseMessageProducerApplication Maven / Gradle / Ivy

There is a newer version: 1.3.4
Show newest version
/*===============================================================
 = Copyright (c) 2021 Birch Framework
 = This program is free software: you can redistribute it and/or modify
 = it under the terms of the GNU General Public License as published by
 = the Free Software Foundation, either version 3 of the License, or
 = any later version.
 = This program is distributed in the hope that it will be useful,
 = but WITHOUT ANY WARRANTY; without even the implied warranty of
 = MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 = GNU General Public License for more details.
 = You should have received a copy of the GNU General Public License
 = along with this program.  If not, see .
 ==============================================================*/
package org.birchframework.bridge;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.IntStream;
import javax.jms.Message;
import javax.jms.QueueConnectionFactory;
import com.google.common.base.Throwables;
import org.birchframework.framework.cli.AbstractCommandLineApplication;
import lombok.extern.slf4j.Slf4j;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.component.jms.JmsMessageType;
import org.apache.camel.spring.boot.SpringBootCamelContext;
import org.apache.commons.cli.Option;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.time.Instant.EPOCH;
import static org.apache.camel.component.jms.JmsMessageType.Text;
import static org.apache.camel.component.jms.JmsMessageType.Bytes;

/**
 * Base class for Spring Boot applications that act as message producers to JMS destinations (currently only supports queues).  Reads command line
 * options and sends messages to specified configurations specified through those options.  Running the concrete subclass application without command line
 * arguments will print usage.
 *
 * This class requires implementation of the abstract methods so as so enrich the messages.  See each method's Javadocs for details.
 */
@ComponentScan(basePackages = "org.springframework.boot.autoconfigure.jackson")
@Slf4j
@SuppressWarnings("AutoBoxing")
public class BaseMessageProducerApplication extends AbstractCommandLineApplication {

   protected static final Option TEMPLATE_FILE_OPTION      = requiredWithArgsOption("t", "template-file", "load-testing template file");
   protected static final Option NUMBER_OF_MESSAGES_OPTION = requiredWithArgsOption("n", "number-of-messages", "number of messages to send");
   protected static final Option QUEUE_OPTION              = requiredWithArgsOption("q", "queue", "queue name");
   protected static final Option APPLY_GROUP_ID_OPTION     = noArgsOption("g", "apply-group-id", "apply group id (boolean)");
   protected static final String JMSXGROUP_ID              = "JMSXGroupID";

   protected static final Set supportedTypes = Set.of(Text, Bytes);
   protected static final ThreadLocal threadLocal    = new ThreadLocal<>();

   private final JmsMessageType   messageType;
   private final ProducerTemplate producerTemplate;

   protected final ApplicationContext springContext;

   /**
    * Initializes this application for text message body type.
    * @param theContext the Camel context
    */
   public BaseMessageProducerApplication(final SpringBootCamelContext theContext) {
      this(Text, theContext);
   }

   /**
    * Initializes this application for the given message body type.  Currently only {@link JmsMessageType#Text} and {@link JmsMessageType#Bytes} are
    * supported.
    * @param theMessageType the message type
    * @param theContext the Camel context
    */
   public BaseMessageProducerApplication(final JmsMessageType theMessageType, final SpringBootCamelContext theContext) {
      if (!supportedTypes.contains(theMessageType)) {
         throw new RuntimeException(String.format("Message type %s is not supported.", theMessageType));
      }
      this.messageType      = theMessageType;
      this.producerTemplate = theContext.createProducerTemplate();
      this.springContext    = theContext.getApplicationContext();
   }

   @Override
   protected List