PageRenderTime 27ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/subprojects/s4-example/src/main/java/org/apache/s4/example/counter/MyApp.java

https://github.com/leoneu/s4-piper
Java | 152 lines | 78 code | 27 blank | 47 comment | 0 complexity | f5f17e95565bed5c2fa2cf1be11bc4b6 MD5 | raw file
  1. /*
  2. * Copyright (c) 2011 Yahoo! Inc. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing,
  10. * software distributed under the License is distributed on an
  11. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  12. * either express or implied. See the License for the specific
  13. * language governing permissions and limitations under the
  14. * License. See accompanying LICENSE file.
  15. */
  16. package org.apache.s4.example.counter;
  17. import java.util.concurrent.TimeUnit;
  18. import org.apache.s4.base.Event;
  19. import org.apache.s4.core.App;
  20. import org.apache.s4.core.Receiver;
  21. import org.apache.s4.core.Sender;
  22. import org.apache.s4.core.Stream;
  23. import com.google.inject.Guice;
  24. import com.google.inject.Injector;
  25. /*
  26. * This is a sample application to test a new S4 API.
  27. * See README file for details.
  28. *
  29. * */
  30. final public class MyApp extends App {
  31. final private int interval = 1;
  32. private GenerateUserEventPE generateUserEventPE;
  33. /*
  34. *
  35. *
  36. * The application graph itself is created in this Class. However, developers may provide tools for creating apps
  37. * which will generate the objects.
  38. *
  39. * IMPORTANT: we create a graph of PE prototypes. The prototype is a class instance that is used as a prototype from
  40. * which all PE instance will be created. The prototype itself is not used as an instance. (Except when the PE is of
  41. * type Singleton PE). To create a data structure for each PE instance you must do it in the method
  42. * ProcessingElement.onCreate().
  43. */
  44. /*
  45. * Build the application graph using POJOs. Don't like it? Write a nice tool.
  46. *
  47. * @see io.s4.App#init()
  48. */
  49. @SuppressWarnings("unchecked")
  50. @Override
  51. protected void onInit() {
  52. /* PE that prints counts to console. */
  53. PrintPE printPE = createPE(PrintPE.class);
  54. Stream<CountEvent> userCountStream = createStream(CountEvent.class);
  55. userCountStream.setName("User Count Stream");
  56. userCountStream.setKey(new CountKeyFinder());
  57. userCountStream.setPE(printPE);
  58. Stream<CountEvent> genderCountStream = createStream(CountEvent.class);
  59. genderCountStream.setName("Gender Count Stream");
  60. genderCountStream.setKey(new CountKeyFinder());
  61. genderCountStream.setPE(printPE);
  62. Stream<CountEvent> ageCountStream = createStream(CountEvent.class);
  63. ageCountStream.setName("Age Count Stream");
  64. ageCountStream.setKey(new CountKeyFinder());
  65. ageCountStream.setPE(printPE);
  66. /* PEs that count events by user, gender, and age. */
  67. CounterPE userCountPE = createPE(CounterPE.class);
  68. userCountPE.setTrigger(Event.class, interval, 10l, TimeUnit.MILLISECONDS);
  69. userCountPE.setCountStream(userCountStream);
  70. CounterPE genderCountPE = createPE(CounterPE.class);
  71. genderCountPE.setTrigger(Event.class, interval, 10l, TimeUnit.MILLISECONDS);
  72. genderCountPE.setCountStream(genderCountStream);
  73. CounterPE ageCountPE = createPE(CounterPE.class);
  74. ageCountPE.setTrigger(Event.class, interval, 10l, TimeUnit.MILLISECONDS);
  75. ageCountPE.setCountStream(ageCountStream);
  76. /* Streams that output user events keyed on user, gender, and age. */
  77. Stream<UserEvent> userStream = createStream(UserEvent.class);
  78. userStream.setName("User Stream");
  79. userStream.setKey(new UserIDKeyFinder());
  80. userStream.setPE(userCountPE);
  81. Stream<UserEvent> genderStream = createStream(UserEvent.class);
  82. genderStream.setName("Gender Stream");
  83. /* It is possible to specify a field name of a primitive type as a string instead of using a KeyFinder object. */
  84. // genderStream.setKey(new GenderKeyFinder());
  85. genderStream.setKey("gender");
  86. genderStream.setPE(genderCountPE);
  87. Stream<UserEvent> ageStream = createStream(UserEvent.class);
  88. ageStream.setName("Age Stream");
  89. ageStream.setKey(new AgeKeyFinder());
  90. ageStream.setPE(ageCountPE);
  91. generateUserEventPE = createPE(GenerateUserEventPE.class);
  92. generateUserEventPE.setStreams(userStream, genderStream, ageStream);
  93. generateUserEventPE.setSingleton(true);
  94. generateUserEventPE.setTimerInterval(1, TimeUnit.MILLISECONDS);
  95. }
  96. /*
  97. * Create and send 200 dummy events of type UserEvent.
  98. *
  99. * @see io.s4.App#start()
  100. */
  101. @Override
  102. protected void onStart() {
  103. }
  104. @Override
  105. protected void onClose() {
  106. System.out.println("Bye.");
  107. }
  108. public static void main(String[] args) {
  109. Injector injector = Guice.createInjector(new Module());
  110. MyApp myApp = injector.getInstance(MyApp.class);
  111. Sender sender = injector.getInstance(Sender.class);
  112. Receiver receiver = injector.getInstance(Receiver.class);
  113. myApp.setCommLayer(sender, receiver);
  114. myApp.init();
  115. myApp.start();
  116. try {
  117. Thread.sleep(1000);
  118. } catch (InterruptedException e) {
  119. // TODO Auto-generated catch block
  120. e.printStackTrace();
  121. }
  122. myApp.close();
  123. receiver.close();
  124. }
  125. }