Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2015. Rick Hightower, Geoff Chandler
   *
   * 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.
  *
  * QBit - The Microservice lib for Java : JSON, WebSocket, REST. Be The Web!
  */
 
 package io.advantageous.qbit.metrics.support;
 
 
 
 import java.util.List;
 
 import static io.advantageous.qbit.client.ClientBuilder.clientBuilder;

Stat Service Builder created by rhightower on 1/28/15.
 
 public class StatServiceBuilder {
 
 
     public static final String QBIT_STAT_SERVICE_BUILDER = "qbit.statservice.builder.";
     private EventManager eventManager;
     private Timer timer = Timer.timer();
     private StatRecorder recorder = new NoOpRecorder();
     private StatReplicator replicator = new NoOpReplicator();
     private final List<StatReplicatorreplicators = new ArrayList<>();
     private ClientBuilder clientBuilder;
     private ServiceQueue serviceQueue;
     private ServiceBuilder serviceBuilder;
     private QueueBuilder sendQueueBuilder;
     private StatServiceImpl statServiceImpl;
 
 
     private String serviceName;
     private String localServiceId;
     private int tallyInterval;
     private int flushInterval;
     private int timeToLiveCheckInterval;
     private int numStats;
 
 
     public StatServiceBuilder(PropertyResolver propertyResolver) {
         this. = propertyResolver.getStringProperty("serviceName""statsService");
         this. = propertyResolver.getStringProperty("localServiceId""");
         this. = propertyResolver.getIntegerProperty("tallyInterval", 100);
         this. = propertyResolver.getIntegerProperty("flushInterval", 333);
         this. = propertyResolver
                 .getIntegerProperty("timeToLiveCheckInterval", 5_000);
         this. = propertyResolver
                 .getIntegerProperty("numStats", 100);
 
     }
 
     public StatServiceBuilder() {
         this(PropertyResolver.createSystemPropertyResolver());
     }
 
     public StatServiceBuilder(final Properties properties) {
         this(PropertyResolver.createPropertiesPropertyResolver(
                 properties));
     }
 
     public static StatServiceBuilder statServiceBuilder() {
         return new StatServiceBuilder();
     }
    public int getTallyInterval() {
        return ;
    }
    public StatServiceBuilder setTallyInterval(int tallyInterval) {
        this. = tallyInterval;
        return this;
    }
    public int getFlushInterval() {
        return ;
    }
    public StatServiceBuilder setFlushInterval(int flushInterval) {
        this. = flushInterval;
        return this;
    }
    public int getTimeToLiveCheckInterval() {
        return ;
    }
    public StatServiceBuilder setTimeToLiveCheckInterval(int timeToLiveCheckInterval) {
        this. = timeToLiveCheckInterval;
        return this;
    }
    public int getNumStats() {
        return ;
    }
    public void setNumStats(int numStats) {
        this. = numStats;
    }
    public QueueBuilder getSendQueueBuilder() {
        if ( == null) {
             = QueueBuilder.queueBuilder().setLinkTransferQueue()
                    .setBatchSize(1_000).setPollWait(500);
        }
        return ;
    }
    public StatServiceBuilder setSendQueueBuilder(QueueBuilder sendQueueBuilder) {
        this. = sendQueueBuilder;
        return this;
    }
        if ( == null) {
             = EndpointServerBuilder.endpointServerBuilder();
        }
        return ;
    }
    public StatServiceBuilder setEndpointServerBuilder(EndpointServerBuilder endpointServerBuilder) {
        this. = endpointServerBuilder;
        return this;
    }
    public ServiceQueue getServiceQueue() {
        if ( == null) {
            buildServiceQueue();
        }
        return ;
    }
    public StatServiceBuilder setServiceQueue(ServiceQueue serviceQueue) {
        this. = serviceQueue;
        return this;
    }
    public ServiceBuilder getServiceBuilder() {
        if ( == null) {
             = ServiceBuilder.serviceBuilder();
        }
        return ;
    }
    public StatServiceBuilder setServiceBuilder(ServiceBuilder serviceBuilder) {
        this. = serviceBuilder;
        return this;
    }
    public String getLocalServiceId() {
        return ;
    }
    public StatServiceBuilder setLocalServiceId(String localServiceId) {
        this. = localServiceId;
        return this;
    }
    public String getServiceName() {
        return ;
    }
    public StatServiceBuilder setServiceName(String serviceName) {
        this. = serviceName;
        return this;
    }
    private ClientBuilder getClientBuilder() {
        if ( == null) {
             = clientBuilder();
        }
        return BeanUtils.copy();
    }
        return ;
    }
    public StatServiceBuilder setServiceDiscovery(ServiceDiscovery serviceDiscovery) {
        this. = serviceDiscovery;
        return this;
    }
    public Timer getTimer() {
        return ;
    }
    public StatServiceBuilder setTimer(Timer timer) {
        this. = timer;
        return this;
    }
    public StatServiceBuilder addReplicator(StatReplicator replicator) {
        .add(replicator);
        return this;
    }
    public StatRecorder getRecorder() {
        return ;
    }
    public StatServiceBuilder setRecorder(StatRecorder recorder) {
        this. = recorder;
        return this;
    }
    public StatReplicator getReplicator() {
        return ;
    }
    public StatServiceBuilder setReplicator(StatReplicator replicator) {
        this. = replicator;
        return this;
    }
    @SuppressWarnings("UnusedReturnValue")
    public ServiceQueue buildServiceQueue() {
        ServiceBuilder serviceBuilder = getServiceBuilder()
                .setRequestQueueBuilder(getSendQueueBuilder())
                .setServiceObject(getStatServiceImpl());
         = serviceBuilder.build();
        if ( != null) {
            if ( != null &&  != QBit.factory().systemEventManager()) {
                .joinService();
            }
        }
        return ;
    }
        final EndpointServerBuilder endpointServerBuilder = getEndpointServerBuilder();
        if ( != null) {
            if ( == null || "".equals(.trim())) {
                 =
                         + "-" + ServiceDiscovery.uniqueString(endpointServerBuilder.getPort());
            }
                    endpointServerBuilder.getPort(), );
        }
        final ServiceQueue serviceQueue = getServiceQueue();
        final ServiceEndpointServer serviceEndpointServer = endpointServerBuilder.build();
        serviceEndpointServer.addServiceObject(this.getServiceName(), serviceQueue.service());
        return serviceEndpointServer;
    }
        if ( == null) {
             = build();
        }
        return ;
    }
    public StatServiceBuilder setStatServiceImpl(StatServiceImpl statServiceImpl) {
        this. = statServiceImpl;
        return this;
    }
    public StatServiceImpl build() {
        final StatReplicator replicator = buildReplicator();
        return new StatServiceImpl(this.getRecorder(), replicatorgetTimer(), getServiceDiscovery(),
                getLocalServiceId(), getNumStats(), getTimeToLiveCheckInterval());
    }
    private StatReplicator buildReplicator() {
        final List<StatReplicatorfinalReplicators = new ArrayList<>();
        /* If service discovery is set then setup clustering
           and add it to the list. */
        if ( != null) {
            finalReplicators.add(buildClusteredReplicator());
        }
        /* If the replicator is not the NoOpReplicator,
          then add it ot the list. */
        if (!(this.getReplicator() instanceof NoOpReplicator)) {
            finalReplicators.add(this.getReplicator());
        }
        /*
            If we have any replicators then add them to the list.
         */
        if (this..size() > 0) {
            finalReplicators.addAll(this.);
        }
        /* If there are some replicators in our list, then create a replicator hub. */
        // then one then create a hub.
        return finalReplicators.size() == 0
                ? new NoOpReplicator() // if 0 then just return a no op
                : finalReplicators.size() == 1 //if one then just use it direct
                ? finalReplicators.get(0)
                : new ReplicatorHub(finalReplicators);
    }
        if ( == null) {
        }
        return ;
    }
    public StatServiceBuilder setStatReplicatorProvider(StatReplicatorProvider statReplicatorProvider) {
        this. = statReplicatorProvider;
        return this;
    }
                getStatsReplicatorProvider(), getLocalServiceId(), getTimer(),
                getTallyInterval(), getFlushInterval());
    }
        return serviceDefinition -> {
            final ClientBuilder clientBuilder1 = getClientBuilder();
            final Client client = clientBuilder1.setPort(serviceDefinition.getPort())
                    .setProtocolBatchSize(1000)
                    .setHost(serviceDefinition.getHost()).build();
            final StatReplicator proxy = client.createProxy(StatReplicator.class);
            client.start();
            return new StatReplicator() {
                private final Client theClient = client;
                @Override
                protected void finalize() throws Throwable {
                    super.finalize();
                    if ( != null) {
                        .stop();
                    }
                }
                @Override
                public void replicateCount(String nameint countlong now) {
                    proxy.replicateCount(namecountnow);
                }
                @Override
                public void replicateLevel(String nameint levellong time) {
                    proxy.replicateLevel(nameleveltime);
                }
                @Override
                public void replicateTiming(String nameint levellong time) {
                    proxy.replicateTiming(nameleveltime);
                }
                @Override
                public void clientProxyFlush() {
                    proxy.clientProxyFlush();
                }
                @Override
                public void stop() {
                    proxy.stop();
                    .stop();
                }
                @Override
                public void flush() {
                    proxy.flush();
                    .flush();
                }
                public String toString() {
                    return "StatServiceReplicator " + proxy;
                }
                @Override
                public int port() {
                    return proxy.port();
                }
                @Override
                public String host() {
                    return proxy.host();
                }
                @Override
                public boolean connected() {
                    return proxy.connected();
                }
                @Override
                public boolean remote() {
                    return proxy.remote();
                }
            };
        };
    }
    public EventManager getEventManager() {
        if ( == null) {
             = QBit.factory().systemEventManager();
        }
        return ;
    }
    public StatServiceBuilder setEventManager(EventManager eventManager) {
        this. = eventManager;
        return this;
    }
        return getServiceQueue().createProxy(StatService.class);
    }
    }
New to GrepCode? Check out our FAQ X