Search in sources :

Example 1 with EntityStateUpdate

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);
}
Also used : Customer(io.spine.test.commandservice.customer.Customer) GivenVersion(io.spine.core.given.GivenVersion) Version(io.spine.core.Version) EntityStateUpdate(io.spine.client.EntityStateUpdate) CustomerId(io.spine.test.commandservice.customer.CustomerId) Topic(io.spine.client.Topic) Map(java.util.Map) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) Test(org.junit.Test) TenantAwareTest(io.spine.server.tenant.TenantAwareTest)

Example 2 with EntityStateUpdate

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;
}
Also used : EntityStateUpdate(io.spine.client.EntityStateUpdate) TypeConverter.toAny(io.spine.protobuf.TypeConverter.toAny) Any(com.google.protobuf.Any)

Example 3 with EntityStateUpdate

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);
    }
}
Also used : Stand(io.spine.server.stand.Stand) EntityStateUpdate(io.spine.client.EntityStateUpdate) SubscriptionUpdate(io.spine.client.SubscriptionUpdate)

Aggregations

EntityStateUpdate (io.spine.client.EntityStateUpdate)3 Maps.newHashMap (com.google.common.collect.Maps.newHashMap)1 Any (com.google.protobuf.Any)1 SubscriptionUpdate (io.spine.client.SubscriptionUpdate)1 Topic (io.spine.client.Topic)1 Version (io.spine.core.Version)1 GivenVersion (io.spine.core.given.GivenVersion)1 TypeConverter.toAny (io.spine.protobuf.TypeConverter.toAny)1 Stand (io.spine.server.stand.Stand)1 TenantAwareTest (io.spine.server.tenant.TenantAwareTest)1 Customer (io.spine.test.commandservice.customer.Customer)1 CustomerId (io.spine.test.commandservice.customer.CustomerId)1 Map (java.util.Map)1 Test (org.junit.Test)1