Last active
October 21, 2018 01:07
-
-
Save renanpalmeira/a1c5672189e9d7fce1a0cf3fde2ba0c6 to your computer and use it in GitHub Desktop.
Prototype of OrderedProcessEvent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Slf4j | |
@Component | |
class FirstListener { | |
@Autowired | |
private ApplicationEventPublisher publisher; | |
@EventListener(condition = "#event.isFirst()") | |
public void handle(OrderedProcessEvent<Model> event) { | |
log.info("Initial ordered process - step 1"); | |
event.nextEvent().ifPresent(publisher::publishEvent); | |
} | |
@EventListener(condition = "#event.step == 2") // or @EventListener(condition = "#event.isSecond()") | |
public void handle1(OrderedProcessEvent<Model> event) { | |
log.info("ordered process - step 2"); | |
event.nextEvent().ifPresent(publisher::publishEvent); | |
} | |
@EventListener(condition = "#event.isThird()") | |
public void handle2(OrderedProcessEvent<Model> event) { | |
log.info("finish process - step 3"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Model implements Serializable { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Getter | |
@Setter | |
@EqualsAndHashCode(callSuper = true) | |
public class OrderedProcessEvent<T extends Serializable> extends ApplicationEvent implements ResolvableTypeProvider { | |
private static final int MAX_STEP = 10; | |
private static final int FIRST = 1; | |
private static final int SECOND = 2; | |
private static final int THIRD = 3; | |
private static final int FOURTH = 4; | |
private static final int FIFTH = 5; | |
private static final int SIXTH = 6; | |
private static final int SEVENTH = 7; | |
private static final int EIGHTH = 8; | |
private static final int NINTH = 9; | |
private static final int TENTH = 10; | |
private T model; | |
private AtomicInteger atomStep = new AtomicInteger(0); | |
public static <T extends Serializable> OrderedProcessEvent<T> start(T model) { | |
return new OrderedProcessEvent<>(FIRST, model); | |
} | |
/** | |
* Event reference Generic | |
* @see <a href="https://github.com/olivergierke/spring-examples/blob/6a9845831eb9c4d31cb0069255173fb70a2baefe/4.2/src/main/java/example/events/EventsSample.java#L118-L133">Github reference</a> | |
*/ | |
public OrderedProcessEvent(T source) { | |
this(FIRST, source); | |
} | |
public OrderedProcessEvent(int step, T source) { | |
super(source); | |
if (!isValidStep(step)) { | |
throw new UnsupportedOperationException(step + " out range 0 and " + MAX_STEP); | |
} | |
setModel(source); | |
atomStep.set(step); | |
} | |
/* | |
* (non-Javadoc) | |
* @see org.springframework.core.ResolvableTypeProvider#getResolvableType() | |
*/ | |
@Override | |
public ResolvableType getResolvableType() { | |
return ResolvableType.forClassWithGenerics(getClass(), ResolvableType.forInstance(source)); | |
} | |
public int getStep() { | |
return atomStep.get(); | |
} | |
public Optional<OrderedProcessEvent<T>> nextEvent() { | |
return Optional.of(atomStep.get()) | |
.map(step -> step + 1) | |
.filter(this::isValidStep) | |
.map(step -> new OrderedProcessEvent<>(step, model)); | |
} | |
private boolean isValidStep(int step) { | |
return step >= 0 && step <= MAX_STEP; | |
} | |
public boolean isFirst() { | |
return getStep() == FIRST; | |
} | |
public boolean isSecond() { | |
return getStep() == SECOND; | |
} | |
public boolean isThird() { | |
return getStep() == THIRD; | |
} | |
public boolean isFourth() { | |
return getStep() == FOURTH; | |
} | |
public boolean isFifth() { | |
return getStep() == FIFTH; | |
} | |
public boolean isSixth() { | |
return getStep() == SIXTH; | |
} | |
public boolean isSeventh() { | |
return getStep() == SEVENTH; | |
} | |
public boolean isEighth() { | |
return getStep() == EIGHTH; | |
} | |
public boolean isNinth() { | |
return getStep() == NINTH; | |
} | |
public boolean isTenth() { | |
return getStep() == TENTH; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RunWith(SpringRunner.class) | |
@SpringBootTest | |
public class Tests { | |
@Autowired | |
private ApplicationEventPublisher publisher; | |
@Test | |
public void testPublishEvent() { | |
publisher.publishEvent(new OrderedProcessEvent<>(new Model())); // option 1 | |
publisher.publishEvent(new OrderedProcessEvent<>(1, new Model())); // option 2 | |
publisher.publishEvent(OrderedProcessEvent.start(new Model())); // option 3 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment