almira.sample.client.ListCatapults.java Source code

Java tutorial

Introduction

Here is the source code for almira.sample.client.ListCatapults.java

Source

/*
 * Copyright 2008-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package almira.sample.client;

import java.util.List;

import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;

import almira.sample.domain.Catapult;
import almira.sample.service.ObjectQueryService;

/**
 * The Class ListCatapults. Connects to a remote catapult service and executes a query.
 * Example: http://localhost:8080/catapult/services/list
 */
public final class ListCatapults {

    private static final int START_INDEX = 0;
    private static final int FETCH_SIZE = 5;
    private static final int NANOS_IN_SEC = 1000000;

    private ListCatapults() {
        // Utility classes should not have a public or default constructor.
    }

    /**
     * Main function.
     *
     * @param args the serviceUrl as command line argument.
     */
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {

        if (args.length != 1) {

            printMessage("Usage: java -jar client.jar <serviceUrl>");
            System.exit(1);
        }

        HttpInvokerProxyFactoryBean fb = getFactoryBeanInstance(args[0]);
        executeQuery((ObjectQueryService<Catapult, Long>) fb.getObject());
    }

    static HttpInvokerProxyFactoryBean getFactoryBeanInstance(String serviceUrl) {
        HttpInvokerProxyFactoryBean fb = new HttpInvokerProxyFactoryBean();
        fb.setServiceInterface(ObjectQueryService.class);
        fb.setServiceUrl(serviceUrl);
        fb.afterPropertiesSet();
        return fb;
    }

    static void executeQuery(ObjectQueryService<Catapult, Long> service) {
        long startTime = System.nanoTime();
        List<Catapult> catapults = service.findAll(START_INDEX, FETCH_SIZE);
        long timeDifference = System.nanoTime() - startTime;

        StringBuilder sb = new StringBuilder();
        sb.append("\nRequesting ");
        sb.append(catapults.size());
        sb.append(" catapults took: ");
        sb.append(timeDifference / NANOS_IN_SEC);
        sb.append(" ms");
        printMessage(sb.toString());

        for (Catapult c : catapults) {
            printMessage(c.toString());
        }
    }

    private static void printMessage(String message) {
        System.out.println(message); // NOPMD
    }
}