001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.tang.examples; 020 021import org.apache.reef.tang.Configuration; 022import org.apache.reef.tang.Injector; 023import org.apache.reef.tang.JavaConfigurationBuilder; 024import org.apache.reef.tang.Tang; 025import org.apache.reef.tang.annotations.Name; 026import org.apache.reef.tang.annotations.NamedParameter; 027import org.apache.reef.tang.annotations.Parameter; 028import org.apache.reef.tang.exceptions.BindException; 029import org.apache.reef.tang.exceptions.InjectionException; 030 031import javax.inject.Inject; 032 033public class TimerV1 { 034 035 private final int seconds; 036 037 @Inject 038 public TimerV1(@Parameter(Seconds.class) final int seconds) { 039 this.seconds = seconds; 040 } 041 042 public static void main(final String[] args) throws BindException, InjectionException { 043 final Tang tang = Tang.Factory.getTang(); 044 final JavaConfigurationBuilder cb = (JavaConfigurationBuilder) tang.newConfigurationBuilder(); 045 final Configuration conf = cb.build(); 046 final Injector injector = tang.newInjector(conf); 047 final TimerV1 timer = injector.getInstance(TimerV1.class); 048 049 try { 050 System.out.println("Tick..."); 051 timer.sleep(); 052 System.out.println("Tock."); 053 } catch (final InterruptedException e) { 054 e.printStackTrace(); 055 } 056 } 057 058 public void sleep() throws InterruptedException { 059 java.lang.Thread.sleep(seconds * 1000); 060 } 061 062 @NamedParameter(default_value = "10", 063 doc = "Number of seconds to sleep", short_name = "sec") 064 class Seconds implements Name<Integer> { 065 } 066}