use of io.spine.client.EntityStateUpdate in project core-java by SpineEventEngine.
the class StandShould method trigger_subscription_callbacks_matching_by_id.
@Test
public void trigger_subscription_callbacks_matching_by_id() {
final Stand stand = prepareStandWithAggregateRepo(mock(StandStorage.class));
final Map<CustomerId, Customer> sampleCustomers = fillSampleCustomers(10);
final Topic someCustomers = requestFactory.topic().someOf(Customer.class, sampleCustomers.keySet());
final Map<CustomerId, Customer> callbackStates = newHashMap();
final MemoizeEntityUpdateCallback callback = new MemoizeEntityUpdateCallback() {
@Override
public void onStateChanged(EntityStateUpdate update) {
super.onStateChanged(update);
final Customer customerInCallback = unpack(update.getState());
final CustomerId customerIdInCallback = unpack(update.getId());
callbackStates.put(customerIdInCallback, customerInCallback);
}
};
subscribeAndActivate(stand, someCustomers, callback);
for (Map.Entry<CustomerId, Customer> sampleEntry : sampleCustomers.entrySet()) {
final CustomerId customerId = sampleEntry.getKey();
final Customer customer = sampleEntry.getValue();
final Version stateVersion = GivenVersion.withNumber(1);
stand.update(asEnvelope(customerId, customer, stateVersion));
}
assertEquals(newHashMap(sampleCustomers), callbackStates);
}
use of io.spine.client.EntityStateUpdate in project core-java by SpineEventEngine.
the class Stand method notifySubscriptionAction.
/**
* Creates the subscribers notification action.
*
* <p>The resulting action retrieves the {@linkplain EntityUpdateCallback subscriber callback}
* and invokes it with the given Entity ID and state.
*
* @param subscriptionRecord the attributes of the target subscription
* @param id the ID of the updated Entity
* @param entityState the new state of the updated Entity
* @return a routine delivering the subscription update to the target subscriber
*/
private static Runnable notifySubscriptionAction(final SubscriptionRecord subscriptionRecord, final Object id, final Any entityState) {
final Runnable result = new Runnable() {
@Override
public void run() {
final EntityUpdateCallback callback = subscriptionRecord.getCallback();
checkNotNull(callback, "Notifying by a non-activated subscription.");
final Any entityId = toAny(id);
final EntityStateUpdate stateUpdate = EntityStateUpdate.newBuilder().setId(entityId).setState(entityState).build();
callback.onStateChanged(stateUpdate);
}
};
return result;
}
use of io.spine.client.EntityStateUpdate in project core-java by SpineEventEngine.
the class SubscriptionService method activate.
@Override
public void activate(final Subscription subscription, final StreamObserver<SubscriptionUpdate> responseObserver) {
log().debug("Activating the subscription: {}", subscription);
try {
final BoundedContext boundedContext = selectBoundedContext(subscription);
final Stand.EntityUpdateCallback updateCallback = new Stand.EntityUpdateCallback() {
@Override
public void onStateChanged(EntityStateUpdate stateUpdate) {
checkNotNull(subscription);
final SubscriptionUpdate update = SubscriptionUpdate.newBuilder().setSubscription(subscription).setResponse(Responses.ok()).addEntityStateUpdates(stateUpdate).build();
responseObserver.onNext(update);
}
};
final Stand targetStand = boundedContext.getStand();
targetStand.activate(subscription, updateCallback, StreamObservers.<Response>forwardErrorsOnly(responseObserver));
} catch (@SuppressWarnings("OverlyBroadCatchBlock") Exception e) {
log().error("Error activating the subscription", e);
responseObserver.onError(e);
}
}
Aggregations