-
Notifications
You must be signed in to change notification settings - Fork 1
Demo 3. Example of a simple Conveyor
aegisql edited this page Feb 11, 2017
·
7 revisions
ThreadPool - Helping multi-thread executor
Modify Demo 2 example to use the Assembling Conveyor code
/*
* COPYRIGHT (C) AEGIS DATA SOLUTIONS, LLC, 2015
*/
package com.aegisql.conveyor.demo.simple_conveyor;
import java.util.Date;
public class Person {
final String firstName;
final String lastName;
final Date dateOfBirth;
public Person(String firstName, String lastName, Date dateOfBirth) {
this.firstName = firstName;
this.lastName = lastName;
this.dateOfBirth = dateOfBirth;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public Date getDateOfBirth() {
return dateOfBirth;
}
@Override
public String toString() {
return "Person [firstName=" + firstName + ", lastName=" + lastName + ", dateOfBirth=" + dateOfBirth + "]";
}
}
/*
* COPYRIGHT (C) AEGIS DATA SOLUTIONS, LLC, 2015
*/
package com.aegisql.conveyor.demo.simple_conveyor;
import java.util.Date;
import java.util.function.Supplier;
public class PersonBuilder implements Supplier<Person> {
private String firstName;
private String lastName;
private Date dateOfBirth;
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
@Override
public Person get() {
return new Person(firstName,lastName,dateOfBirth);
}
}
ThreadPool pool = new ThreadPool();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
AtomicReference<Person> personRef = new AtomicReference<>();
// I - Create conveyor
// Generic types:
// Integer - type of the unique build ID
// String - type of the message labels
// Person - type of the Product
Conveyor<Integer, String, Person> conveyor = new AssemblingConveyor<>();
// II - Tell conveyor how to create the Builder
conveyor.setBuilderSupplier(PersonBuilder::new);
// III - Explain conveyor how to process Building Parts
conveyor.setDefaultCartConsumer(Conveyor.getConsumerFor(conveyor)
.when("FirstName", (builder,value)->{
((PersonBuilder) builder).setFirstName((String)value);
})
.when("LastName", (builder,value)->{
((PersonBuilder) builder).setLastName((String)value);
})
//note how we tell the filter to ignore label case
.filter((l)->"dateofbirth".equalsIgnoreCase(l), (builder,value)->{
((PersonBuilder) builder).setDateOfBirth((Date) value);
})
);
// IV - Build is ready when builder accepted three different pieces of data
conveyor.setReadinessEvaluator(Conveyor.getTesterFor(conveyor).accepted(3));
// V - Tell conveyor where to put created Person object
// Product receiver should not block the thread
conveyor.setResultConsumer( bin-> personRef.set(bin.product) );
// VI - Optionally: retrieve completable future of the build
CompletableFuture<Person> future = conveyor.createBuildFuture(1);
// VII - Send data to conveyor asynchronously
pool.runAsynchWithDelay(10,()->{
conveyor;
.part()
.value("John")
.id(1)
.label("FirstName")
.place(); }
);
pool.runAsynchWithDelay(10,()->{
conveyor
.part()
.value("Silver")
.id(1)
.label("LastName")
.place();
}
);
pool.runAsynchWithDelay(10,()->{
try {
conveyor
.part()
.id(1)
.value(format.parse("1695-11-10"))
.label("DateOfBirth")
.place();
} catch (Exception e) {}
}
);
// No guess work. Method get() of the future
// Will return result as soon as its built
// and sent to the consumer
Person person = future.get();
System.out.println( "Person from asynchronous source: "+personRef.get() );
System.out.println( "Person synchronized: "+person );
pool.shutdown();
conveyor.stop();
Person from asynchronous source: Person [firstName=John, lastName=Silver, dateOfBirth=Thu Nov 10 00:00:00 EST 1695]
Person synchronized: Person [firstName=John, lastName=Silver, dateOfBirth=Thu Nov 10 00:00:00 EST 1695]
INFO:417 [AssemblingConveyor 11] - Leaving AssemblingConveyor 11