How can I persist an object created at a specific step of a Flux, and will not be needed after?

stepverifier examples
spring webflux
spring webflux crud example
project reactor
flux vs mono
webflux get value from mono
flux collectlist
spring reactive

I'm creating an object Employee, and I also want to create another object Event which acts as a log of the creation of the employee. However, the way I'm doing it does not persist the Event.

    @Transactional
    public Mono<Employee> createEmployee(String name) {

        return Mono.fromCallable(() -> {
            Employee employee = new Employee();
            employee.setName(name);
            return employee;
        })
        .flatMap(employee -> {
            return employeeRepository.save(employee);
        })
        .doOnNext(employee -> {
            Event event = new Event();
            event.setDetail("Created employee : " + employee.getName());

            eventRepository
                .save(event)
                // .subscribe() // that didn't help
                ;
        });
    }

Repositories :

@Repository
public interface EmployeeRepository extends ReactiveCrudRepository<Employee, UUID> {
}

@Repository
public interface EventRepository extends ReactiveCrudRepository<Event, UUID> {
}

The employee will be saved, but not the event. I'm not really sure how this should be coded.

Like this instead:

 .flatMap(employee -> {
        return employeeRepository.save(employee);
    })
 .flatMap(employee -> {
        Event event = new Event();
        event.setDetail("Created employee : " + employee.getName());

        eventRepository
            .save(event)
            .thenReturn(employee);
    })

So, this way you get a fresh Employee after saving in the first flatMap(), propagate it downstream to the next flatMap(), call .save(event) for returning its Mono, but then you replace that Mono with a new one based on an employee variable.

This way everything is going to be subscribed at the right time in right order.

The Presocratics, The suspect version states: 'You could not step twice into the same river. is known that Cratylus was concerned to deny that any kind of persisting object was to almost certain that the second version of the 'river', and the 'flux doctrine', are This process repeatsitselfevery day, a different bowl being needed for each day. You can use property bags to persist any key-value pairs in order to store settings for your application. Using property bags, you can persist page settings in the following scopes: Personalization : User-specific settings (These settings supersede Customizing and Configuration settings.)

If you have a reactive treatment inside doOnNext, it will never be run. So, it's up to you to make the subscription. But I think the best is to use the solution of @Artem Bilan.

Identity Issues: Literary and Linguistic Landscapes, Every object is subject to change and is, indeed, always undergoing some kind of change or other. The person who steps into the river the second time is likewise not the same All things are in the state of flux, or becoming, according to Hall. clear) it does not follow that he had to deny that there are persisting objects. Collect incoming values into multiple user-defined Collection buffers that will be emitted by the returned Flux each time the given max size is reached or once this Flux completes. Buffers can be created with gaps, as a new buffer will be created every time skip values have been emitted by the source When maxSize skip : dropping buffers

This should work:

@Transactional
public Mono<Employee> createEmployee(String name) {

    return Mono.fromCallable(() -> {
        Employee employee = new Employee();
        employee.setName(name);
        return employee;
    })
    .flatMap(employee -> {
        return employeeRepository.save(employee);
    })
    .doOnNext(employee -> {
        Event event = new Event();
        event.setDetail("Created employee : " + employee.getName());

        eventRepository
            .save(event)
            .block() // that didn't help
            ;
    });
}

The Rise and Fall of Soul and Self: An Intellectual History of , Probably he meant that because all material objects are always changing by Plato and Aristotle to have carried Heraclitus's intriguing idea one step further, is constantly changing, not only does nothing persist but it is not even possible believed that since everything is in constant flux, humans too are in constant flux. Having created a bitmap object the next step is create a Graphics Object for the bitmap so that we can draw graphics into the bitmap. This is achieved by calling the Graphics.FromImage() method, passing through the bitmap as the argument. Our Form1_Load() method should now appear as follows:

Lighting Design: A Perception-Based Approach, There is more about this perceived difference effect in the following chapter. of the total indirect illuminance received by an object or surface within the space, unit is defined as the unit of illuminance, and so should not be used for exitance. it is the luminous flux due to a small element in a given direction, relative to the​  Location-specific microbes in principle offer a cheap and sensitive way to determine object provenance. We created a synthetic, scalable microbial spore system that identifies object provenance in under 1 hour at meter-scale resolution and near single-spore sensitivity and can be safely introduced into and recovered from the environment.

The Forceps: Journal of Dental Surgery, the Collateral Arts and , In proof of this , I might point your RHEA , OR SALIVARY FLUX . attention to the yet perbody , termed the gizzard , performs the office and after all treatment was given up . bird may be observed the disease has persisted . cealed the nose , and the mouth was But in how many cases does not this portion of the other . You can use this interface to make Jot store data anywhere you like e.g. in the cloud (to share settings for a user between machines) or a database. Value conversions and cancellation. Jot lets you hook into the Apply and Persist operations. You can use this to perform value conversion and cancel persisting or applying data.

New Scientist, Roberts and E. Sebestyen at the Division of Textile Physics, Sydney, have dispelled fibre strength by about 5 per cent, and this increase persisted after scouring. in the efficiency of voltage step-down transformers and similar equipment will with which magnetic flux can be made to flow through silicon iron — the basic  When you insert data in a table during development, the SQL statement you use to insert the data is cached in the AOS. Next you might add a new field to the table and persist the change to the database. This causes the SQL statement in the cache to become stale, because the statement is not updated to include the new field.

Comments
  • Thanks ! Works nicely. However the "why" is a bit confusing to me. I understand that in the end Fluxes/Monos need to be subscribed in order to be "processed", but why couldn't I just trigger the .save() with a .subscribe() right after, just to say "Hey someone's listening, so do your thing" ? Does this "sub-stream" have to be eventually part of the main stream that ends up with the final return statement ? There's a few bits of this process that I don't get :) thanks !
  • Well, I think it depends from case to case, but I believe that your eventRepository.save(event) deals with some data base, like MongoDB. There is no guarantee that its Mono for subscription is going to be performed in the same process and in the proper order as your main stream: the subscription to the Publisher doesn't mean an immediate execution. And I guess in your case there is some buffer for DB commands to handle, so your out of order subscription might just break something over there.
  • Time to accept the answer? stackoverflow.com/help/someone-answers
  • How's this different from what the OP is doing? In the .doOnNext(), he's subscribing to the inner stream, right? It means it should be triggered. This is exactly what .flatMap is doing. I think doing a .block instead of .subscribe inside the doOnNext should work pretty fine. This would ensure that the function returns only when the event is persisted.
  • Why the downvote? This code will work perfectly. The doOnNext would not move forward until the persist takes place. This is ensured by .block().
  • You have to avoid the use of block, you can have an exception like this: java.lang.IllegalStateException: block () / blockFirst () / blockLast () are blocking, which is not supported in thread-http- nio-4. look at this question: stackoverflow.com/questions/51449889/…