org.tocode.poc.service.PocService.java Source code

Java tutorial

Introduction

Here is the source code for org.tocode.poc.service.PocService.java

Source

/* ----------------------------------------------------------------------------
 * All rights reserved  2017 salvadorgnolasco.
 *  
 * 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
 * (at your option) 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 <http://www.gnu.org/licenses/>.
 * ---------------------------------------------------------------------------
 * File name: PocService.java
 * Original Author: Salvador Gonzalez Nolasco.
 * Creation Date: 12/02/2017
 * ---------------------------------------------------------------------------
 */
package org.tocode.poc.service;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.tocode.poc.aggregator.IAggregator;
import org.tocode.poc.formatter.IFormatter;
import org.tocode.poc.response.TransactionResponse;
import org.tocode.poc.validator.QueryResponseValidator;

/**
 * The Class PocService.
 */
@Service
public class PocService {

    /** The aggregator. */
    private IAggregator aggregator;

    /** The formatter. */
    private IFormatter formatter;

    /** The validator. */
    private QueryResponseValidator validator;

    /**
     * Instantiates a new poc service.
     */
    public PocService() {

        this.validator = new QueryResponseValidator();
    }

    /**
     * Sets the aggregator.
     *
     * @param aggregator
     *            the new aggregator
     */
    public void setAggregator(IAggregator aggregator) {

        this.aggregator = aggregator;
    }

    /**
     * Sets the formatter.
     *
     * @param formatter
     *            the new formatter
     */
    public void setFormatter(IFormatter formatter) {

        this.formatter = formatter;
    }

    /**
     * Execute service.
     *
     * @return the transaction response
     */
    public TransactionResponse executeService() {

        TransactionResponse response = null;
        String bufferResponse = aggregator.executeQuery();

        if (validator.isAValidResponse(bufferResponse, aggregator.parameterNumber())) {
            response = formatter.format(bufferResponse);
        }

        return response;
    }

    /**
     * Execute async.
     */
    @Async
    public void executeAsync() {

        for (int iteration = 0; iteration < 20; iteration++) {

            try {
                System.out.println("Service Iteration: " + iteration);
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}