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

com.siemensqms.qmsplus.sdk.receive.MessageHandlerDispatcher Maven / Gradle / Ivy

The newest version!
package com.siemensqms.qmsplus.sdk.receive;

import com.siemensqms.qmsplus.sdk.bean.MessagePayload;
import com.siemensqms.qmsplus.sdk.bean.MessageResponse;
import com.siemensqms.qmsplus.sdk.runner.AdapterInitRunner;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * The class will receive {@link MessagePayload} from gateway, then find message consume class by
 * message topicId. The message content will be handled to a {@code List} with Javabean if the
 * content is a json. Otherwise, it will be handled as a string to dispatch consumer class which
 * related to topic.
 *
 * @author Sherry.
 * @author Peter(last update).
 */

@RestController
@RequestMapping("/gateway")
public class MessageHandlerDispatcher {

  private static final Logger logger = LoggerFactory.getLogger(MessageHandlerDispatcher.class);

  /**
   * The function will dispatch message content to process message function in consumer class. The
   * method is obtained by reflection.
   *
   * @param messagePayload Message request.
   * @param consumer       The consumer class which upward transform into {@code IConsumer}.
   * @return A boolean value marks dispatch message success or not.
   * @author Sherry.
   */
  public static boolean forwardMessageToConsumer(MessagePayload messagePayload,
                                                 IConsumer consumer) throws Exception {

    boolean isForward = true;
    consumer.handle(messagePayload);

    return isForward;
  }

  /**
   * The function is used to receive message from gateway and forward to the corresponding
   * processing class of message topicId.
   *
   * @param messagePayload The content of message request which receives from gateway.
   * @return A String value marks message dispatch is successful or failed.
   * @author Sherry.
   */
  @PostMapping(value = "/callback", consumes = "application/json;charset=UTF-8")
  public MessageResponse callbackJson(@RequestBody MessagePayload messagePayload) throws Exception {

    String topicId = messagePayload.getTopicId();

    logger.info("Now doing consuming. The information of the messageId is {} .", messagePayload);

    IConsumer consumer;

    MessageResponse messageResponse = new MessageResponse();
    /**
     * If a topicId has been recorded in consumerMap, then get consumer class. Otherwise, return
     * false because sdk can not find consumer class which related to topicId.
     */
    if (AdapterInitRunner.consumerMap.containsKey(topicId)) {
      Class consumerClazz = AdapterInitRunner.consumerMap.get(topicId);

      consumer = (IConsumer) consumerClazz.newInstance();

    } else {
      String errorMsg = "Can not find the consumer with topicId: " + topicId + ".";
      messageResponse.setCode(50002);
      messageResponse.setStatus(500);
      messageResponse.setShortMessage(errorMsg);
      logger.error("Error happened with the messageResponse: {}.", messageResponse);
      return messageResponse;
    }

    forwardMessageToConsumer(messagePayload, consumer);

    messageResponse.setCode(200);
    messageResponse.setStatus(200);
    messageResponse.setShortMessage("success");
    logger.info("A success message received with the messageResponse: {}.", messageResponse);
    return messageResponse;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy