A demand

The system requires to import packet data. The data is imported into the system in the form of Excel. The packets are divided into three types.

The second analysis

The packets are divided into three types and are in Excel format. The packets can be used as an abstraction and imported in template mode

Three practice

  1. Create a new abstract class IExcelOutYard

     public interface IExcelOutYard {
    
         String messageType();
    
         void loadExcel(Exchange exchange, String fileName, String camelFileParent);
     }
    Copy the code
  2. New abstract class excelOutYardImplements IExcelOutYard ““ @service public Abstract class ExcelOutYardAbstract implements IExcelOutYard {

    private static Logger logger = LogManager.getCurrentClassLogger(); @Override public void loadExcel(Exchange exchange, String fileName,String camelFileParent) { Object body = exchange.getIn().getBody(); GenericFile<File> genericFile = (GenericFile<File>) body; File file = genericFile.getFile(); this.analysis(file, fileName,camelFileParent); } private void analysis(File file, String fileName,String camelFileParent) { Workbook workbook = getWorkbook(file); Sheet sheet = workbook.getSheetAt(0); Set<String> messages = Sets.newConcurrentHashSet(); // 1. Validate data validateMessage(sheet, messages, fileName); If (objectutils.isnotempty (messages)) {logger.error(" Check failed, cause [{}]", joiner. on(";") ).join(messages)); List<Booking> bookings = contrustData(sheet, fileName,camelFileParent); // 3 Insert data saveAndUpdateData(bookings); } public List<Booking> contrustData(Sheet sheet, String fileName,String camelFileParent) { return null; } public void saveAndUpdateData(List<Booking> bookings) { } public void validateMessage(Sheet sheet, Set<String> messages, String fileName) { } private Workbook getWorkbook(File file) { ----- } } ```Copy the code
    1. New ExcelOutYardAccessGate, ExcelOutYardEmpty, ExcelOutYardSo ExcelOutYardAbstract inherited abstract class

      @Service
      public class ExcelOutYardSo extends ExcelOutYardAbstract {
      
      
          @Override
          public String messageType() {
              return ExcelOutYardEnum.OUT_YARD_SO.name();
          }
      Copy the code
    2. Create a new ExcelOutYardManager to implement the ApplicationContextAware interface

      public class ExcelOutYardManager implements ApplicationContextAware {
      
          Map<String,IExcelOutYard> excelOutYardMap;
      
          private Map<String, IExcelOutYard> getExcelOutYard() {
              if (excelOutYardMap == null) {
                  excelOutYardMap = new HashMap<String, IExcelOutYard>();
              }
              return excelOutYardMap;
          }
      
          public IExcelOutYard getImpleService(String messageType){
              return excelOutYardMap.get(messageType);
          }
      
          @Override
          public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
              Map<String, IExcelOutYard> beansOfType = applicationContext.getBeansOfType(IExcelOutYard.class);
              this.getExcelOutYard();
              for (IExcelOutYard iExcelOutYard:beansOfType.values()) {
                  excelOutYardMap.put(iExcelOutYard.messageType(),iExcelOutYard);
              }
          }
      }
      Copy the code

5. Call

``` String messageType = (String) exchange.getProperty("messageType"); excelOutYardManager.getImpleService(messageType).loadExcel(exchange, fileName,camelFileParent); ` ` `Copy the code

Three conclusion

The ApplicationContextAware interface was introduced in my previous article. After implementing the ApplicationContextAware interface, we can get all the beans in the Bean container. Then we can map the types through the abstract class and its implementation. All you need to do is pass in the type code, enabling a design that conforms to the single-responsibility and open-closed principles.