public static <Config, T> Future<T> start(Flow<Config> flow, final scala.Function1<Flow<Config>, T> fn) { final Promise<T> result = Promise$.MODULE$.<T>apply(); flow.addListener(new FlowListener() { public void onStarting(Flow f) { } // ignore public void onStopping(Flow f) { } // ignore public void onCompleted(Flow f) { // This is always called, but onThrowable is called first if(!result.isCompleted()) { // we use the above rather than trySuccess to avoid calling fn twice try { T toPut = (T) fn.apply(f); result.success(toPut); } catch(Throwable t) { result.failure(t); } } } public boolean onThrowable(Flow f, Throwable t) { result.failure(t); // The exception is handled by the owner of the promise and should not be rethrown return true; } }); flow.start(); return result.future(); } }
public static <Config, T> Future<T> start(Flow<Config> flow, final scala.Function1<Flow<Config>, T> fn) { final Promise<T> result = Promise$.MODULE$.<T>apply(); flow.addListener(new FlowListener() { public void onStarting(Flow f) { } // ignore public void onStopping(Flow f) { } // ignore public void onCompleted(Flow f) { // This is always called, but onThrowable is called first if(!result.isCompleted()) { // we use the above rather than trySuccess to avoid calling fn twice try { T toPut = (T) fn.apply(f); result.success(toPut); } catch(Throwable t) { result.failure(t); } } } public boolean onThrowable(Flow f, Throwable t) { result.failure(t); // The exception is handled by the owner of the promise and should not be rethrown return true; } }); flow.start(); return result.future(); } }
public class EventSystemDemo extends Application { @Override public void start(Stage primaryStage) throws Exception { HBox box = new HBox(); box.setSpacing(12); box.setPadding(new Insets(12)); box.setFillHeight(true); box.setAlignment(Pos.CENTER); Flow senderFlow = new Flow(ProducerController.class); box.getChildren().add(senderFlow.start()); Flow receiverFlow = new Flow(ReceiverController.class); box.getChildren().add(receiverFlow.start()); primaryStage.setScene(new Scene(box)); primaryStage.show(); } public static void main(String... args) { launch(args); } }
flow.start();
@Test public void testProcessFlowFlowListenerExceptionHandlingInStart() throws IOException, InterruptedException { ThrowableListener listener = new ThrowableListener(); getPlatform().copyFromLocal( inputFileIps ); String path = "startException"; Flow process = flowWithException( path, FailingRiffle.Failing.START ); process.addListener( listener ); try { process.start(); fail( "there should have been an exception" ); } catch( CascadingException exception ) { assertNotNull( listener.getThrowable() ); } }
@Test public void testProcessFlowFlowListenerExceptionHandlingInStart() throws IOException, InterruptedException { ThrowableListener listener = new ThrowableListener(); getPlatform().copyFromLocal( inputFileIps ); String path = "startException"; Flow process = flowWithException( path, FailingRiffle.Failing.START ); process.addListener( listener ); try { process.start(); fail( "there should have been an exception" ); } catch( CascadingException exception ) { assertNotNull( listener.getThrowable() ); } }
flow.start();
@Test public void testProcessFlowFlowListenerExceptionHandlingInStop() throws IOException, InterruptedException { ThrowableListener listener = new ThrowableListener(); getPlatform().copyFromLocal( inputFileIps ); String path = "stopException"; Flow process = flowWithException( path, FailingRiffle.Failing.STOP ); process.addListener( listener ); process.start(); try { process.stop(); fail( "there should have been an exception" ); } catch( CascadingException exception ) { assertNotNull( listener.getThrowable() ); } }
parsedLogFlow.start();
@Test public void testProcessFlowFlowListenerExceptionHandlingInStop() throws IOException, InterruptedException { ThrowableListener listener = new ThrowableListener(); getPlatform().copyFromLocal( inputFileIps ); String path = "stopException"; Flow process = flowWithException( path, FailingRiffle.Failing.STOP ); process.addListener( listener ); process.start(); try { process.stop(); fail( "there should have been an exception" ); } catch( CascadingException exception ) { assertNotNull( listener.getThrowable() ); } }
@Test public void testStartWithoutComplete() throws Exception { getPlatform().copyFromLocal( inputFileLower ); Tap sourceLower = new Hfs( new TextLine( new Fields( "offset", "line" ) ), inputFileLower ); Map sources = new HashMap(); sources.put( "lower", sourceLower ); Function splitter = new RegexSplitter( new Fields( "num", "char" ), " " ); // using null pos so all fields are written Tap sink = new Hfs( new TextLine(), getOutputPath( "withoutcomplete" ), SinkMode.REPLACE ); Pipe pipeLower = new Each( new Pipe( "lower" ), new Fields( "line" ), splitter ); pipeLower = new GroupBy( pipeLower, new Fields( "num" ) ); Flow flow = getPlatform().getFlowConnector( getProperties() ).connect( sources, sink, pipeLower ); LockingFlowListener listener = new LockingFlowListener(); flow.addListener( listener ); flow.start(); assertTrue( listener.completed.tryAcquire( 90, TimeUnit.SECONDS ) ); }
@Test public void testStartWithoutComplete() throws Exception { getPlatform().copyFromLocal( inputFileLower ); Tap sourceLower = new Hfs( new TextLine( new Fields( "offset", "line" ) ), inputFileLower ); Map sources = new HashMap(); sources.put( "lower", sourceLower ); Function splitter = new RegexSplitter( new Fields( "num", "char" ), " " ); // using null pos so all fields are written Tap sink = new Hfs( new TextLine(), getOutputPath( "withoutcomplete" ), SinkMode.REPLACE ); Pipe pipeLower = new Each( new Pipe( "lower" ), new Fields( "line" ), splitter ); pipeLower = new GroupBy( pipeLower, new Fields( "num" ) ); Flow flow = getPlatform().getFlowConnector( getProperties() ).connect( sources, sink, pipeLower ); LockingFlowListener listener = new LockingFlowListener(); flow.addListener( listener ); flow.start(); assertTrue( listener.completed.tryAcquire( 90, TimeUnit.SECONDS ) ); }
flow.start();
void runTestCount( String name, Fields argumentSelector, Fields fieldDeclaration, Fields outputSelector ) throws Exception { getPlatform().copyFromLocal( inputFileIps ); Tap source = getPlatform().getTextFile( Fields.size( 2 ), inputFileIps ); Tap sink = getPlatform().getTextFile( Fields.size( 1 ), getOutputPath( name ), SinkMode.REPLACE ); Pipe pipe = new Pipe( "count" ); pipe = new GroupBy( pipe, new Fields( 1 ) ); pipe = new Every( pipe, argumentSelector, new Count( fieldDeclaration ), outputSelector ); Flow flow = getPlatform().getFlowConnector().connect( source, sink, pipe ); flow.start(); // simple test for start flow.complete(); validateLength( flow, 17 ); assertTrue( getSinkAsList( flow ).contains( new Tuple( "63.123.238.8\t2" ) ) ); }
@Test public void testStartStopRace() throws Exception { getPlatform().copyFromLocal( inputFileLower ); Tap sourceLower = new Hfs( new TextLine( new Fields( "offset", "line" ) ), inputFileLower ); Map sources = new HashMap(); sources.put( "lower", sourceLower ); Function splitter = new RegexSplitter( new Fields( "num", "char" ), " " ); // using null pos so all fields are written Tap sink = new Hfs( new TextLine(), getOutputPath( "startstop" ), SinkMode.REPLACE ); Pipe pipeLower = new Each( new Pipe( "lower" ), new Fields( "line" ), splitter ); pipeLower = new GroupBy( pipeLower, new Fields( "num" ) ); Flow flow = getPlatform().getFlowConnector( getProperties() ).connect( sources, sink, pipeLower ); flow.start(); flow.stop(); // should not fail }
@Test public void testStartStopRace() throws Exception { getPlatform().copyFromLocal( inputFileLower ); Tap sourceLower = new Hfs( new TextLine( new Fields( "offset", "line" ) ), inputFileLower ); Map sources = new HashMap(); sources.put( "lower", sourceLower ); Function splitter = new RegexSplitter( new Fields( "num", "char" ), " " ); // using null pos so all fields are written Tap sink = new Hfs( new TextLine(), getOutputPath( "startstop" ), SinkMode.REPLACE ); Pipe pipeLower = new Each( new Pipe( "lower" ), new Fields( "line" ), splitter ); pipeLower = new GroupBy( pipeLower, new Fields( "num" ) ); Flow flow = getPlatform().getFlowConnector( getProperties() ).connect( sources, sink, pipeLower ); flow.start(); flow.stop(); // should not fail }
void runTestCount( String name, Fields argumentSelector, Fields fieldDeclaration, Fields outputSelector ) throws Exception { getPlatform().copyFromLocal( inputFileIps ); Tap source = getPlatform().getTextFile( Fields.size( 2 ), inputFileIps ); Tap sink = getPlatform().getTextFile( Fields.size( 1 ), getOutputPath( name ), SinkMode.REPLACE ); Pipe pipe = new Pipe( "count" ); pipe = new GroupBy( pipe, new Fields( 1 ) ); pipe = new Every( pipe, argumentSelector, new Count( fieldDeclaration ), outputSelector ); Flow flow = getPlatform().getFlowConnector().connect( source, sink, pipe ); flow.start(); // simple test for start flow.complete(); validateLength( flow, 17 ); assertTrue( getSinkAsList( flow ).contains( new Tuple( "63.123.238.8\t2" ) ) ); }