Demo 4. Conveyor with Smart Labels - aegisql/conveyor GitHub Wiki

Demo 4. Conveyor with Smart Labels

Demo Code

ThreadPool - Helping multi-thread executor

Goal

Modify Demo 3 example to use Smart Labels instead of the Default Consumer.

Product class

/* 
 * 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 + "]";
	}
	
}

Builder class

/* 
 * 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>, Testing {
	
	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);
	}
	@Override
	public boolean test() {
		return firstName!=null && lastName != null && dateOfBirth != null;
	}
}

Demo code

ThreadPool pool                   = new ThreadPool();
SimpleDateFormat format           = new SimpleDateFormat("yyyy-MM-dd");
AtomicReference<Person> personRef = new AtomicReference<>();
		
// I - Create labels describing building steps
final SmartLabel<PersonBuilder> FIRST_NAME    
        = SmartLabel.of("FIRST_NAME",PersonBuilder::setFirstName);
final SmartLabel<PersonBuilder> LAST_NAME     
        = SmartLabel.of("LAST_NAME",PersonBuilder::setLastName);
final SmartLabel<PersonBuilder> DATE_OF_BIRTH 
        = SmartLabel.of("DATE_OF_BIRTH",PersonBuilder::setDateOfBirth);
		
// II - Create conveyor
Conveyor<Integer, SmartLabel<PersonBuilder>, Person> conveyor = new AssemblingConveyor<>();

// III - Tell it how to create the Builder
conveyor.setBuilderSupplier(PersonBuilder::new);

// IV - Tell it where to put the Product (asynchronously)
conveyor.setResultConsumer( bin-> personRef.set(bin.product) );

// V - Add data to conveyor queue 
pool.runAsynchWithDelay(10,()->{
	conveyor.part().id(1).value("John").label(FIRST_NAME).place();
	}
);
pool.runAsynchWithDelay(20,()->{
	conveyor.part().id(1).value("Silver").label(LAST_NAME).place();
	}
);
pool.runAsynchWithDelay(50,()->{
	try {
		conveyor.part().id(1).value(format.parse("1695-11-10")).label(DATE_OF_BIRTH).place();
	} catch (Exception e) {}
	}
);

// VI - Optionally - get future of existing build
CompletableFuture<Person> future = conveyor.getFuture(1);

Person person = future.get();

System.out.println( person );

pool.shutdown();
conveyor.stop();

Output

Person [firstName=John, lastName=Silver, dateOfBirth=Thu Nov 10 00:00:00 EST 1695]
INFO:417 [AssemblingConveyor 11] - Leaving AssemblingConveyor 11
⚠️ **GitHub.com Fallback** ⚠️