Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm working on a report of F# async workflows, many papers I found talk about async programming model and patterns. In my understanding async workflows is just part of async programming, and async patterns are tools to write async code. I wish to know more clear relation among these terms, and what should I include in my report(as I only need to discuss async wkf only). Thank you!
Asynchronous programming is a general term for out-of-sync execution, while asynchronous workflows in F# specifically refers to the async computation expression builder and the Async functions. In other words, your understanding is correct: F# async workflows are a tool for the implementation of asynchronous programs.
From the book Expert F#, on terminology:
Asynchronous programs perform requests that don't complete immediately but are fulfilled at a later time and where the program issuing the request has to do meaningful work in the meantime. For example, most network I/O (...)
A report on F# Asyncs may need a brief introduction to asynchronous programming and the usual difficulties with it, since many of the problems Asyncs solve are not issues in typical single-threaded programming. (e.g. exception propagation, memory model, thread management and blocking, cancellation)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
My question addresses a conceptual issue with consumer driven contracts (CDCs), not with Spring-Cloud-Contract in particular (though tagged otherwise).
A CDC usually guarantees a particular behavior of a service (producer/supplier) to its client (consumer). Many architectures (e.g. Hexagonal/Clean/Onion Architecture) have an adapter/layer to provide the technical interface(s) of the service (REST/SOAP/Messaging/...). They separate the adapter from the business logic. When tests are written based on a CDC it is possible to just test the adapter (classical unit test) and mock away the business logic.
However, the contract formally does not only specify a particular mapping of the result (from the business logic) to the data transfer object as returned by the adapter. Moreover it contains semantics wrt. to the request/response behavior: If the consumer sends a specific request with meaningful data, the producer should respond with a particular correct answer (with respect to the request).
My point for discussion/question is: Should the test on the producer side then be an integration test (integrating business logic) to check if the business logic will return the correct answer? In this case the next layer (behind business logic) might be mocked away as long as the business logic is an integral part of the test case.
In Spring CDCs (but I think in other CDC implementations as well) it is possible to distinguish between consumer and producer side and the generated test cases. For example it would be possible to reduce the producer part to check for formal correctness of the response (e.g., if a particular data item is contained and validates against a pattern) while it specifies a concrete response for the consumer part at the same time. This might lead to the (mis-) understanding on the consumer side (and backed by the consumer tests) that the concrete specified data would be returned by the producer if the consumer had sent the specified request from the CDC.
What do users of CDCs think? How do you specify behavior and to what extent do you test it on the producer side?
We describe it in the documentation - https://docs.spring.io/spring-cloud-contract/docs/2.2.5.RELEASE/reference/htmlsingle/#getting-started-introducing-spring-cloud-contract-purposes . Let me write it here for your convenience
Spring Cloud Contract’s purpose is NOT to start writing business
features in the contracts. Assume that we have a business use case of
fraud check. If a user can be a fraud for 100 different reasons, we
would assume that you would create two contracts, one for the positive
case and one for the negative case. Contract tests are used to test
contracts between applications and not to simulate full behavior.
If you check Pact's documentation (https://docs.pact.io/faq#do-i-still-need-end-to-end-tests) you'll find
Contract tests replace a certain class of system integration test (the
ones you do to make sure that you're using the API correctly and that
the API responds the way you expect). They don't replace the tests
that ensure that the core business logic of your services is working.
In case of Spring Cloud Contract - if you have a controller that delegates work to a service, then you should mock out the service and test only whether your controller can take a request and send back a proper response
We have a Kafka listener consuming messages from topic. We want to make this bean as functional so we can spin up multiple instances of function using server less architecture when there is heavy load. Can anyone show me a right direction
Take a look at Spring Cloud Stream Function support. You can use it with a Kafka binder.
In the previous post, I tried to provide justification for our shift to a functional programming model in Spring Cloud Stream (SCSt). It’s less code, less configuration. Most importantly, though, your code is completely decoupled and independent from the internals of SCSt.
In this post, I’ll dig a little deeper and summarize the core features of our functional support, specifically around its reactive features.
As stated in the ReactiveX Introduction - Observables Are Less Opinionated
ReactiveX is not biased toward some particular source of concurrency or asynchronicity. Observables can be implemented using thread-pools, event loops, non-blocking I/O, actors (such as from Akka), or whatever implementation suits your needs, your style, or your expertise. Client code treats all of its interactions with Observables as asynchronous, whether your underlying implementation is blocking or non-blocking and however you choose to implement it.
I am not getting the part - "whether your underlying implementation is blocking or non-blocking".
Can you explain more of it? Or some example code to explain this?
Observable.fromCallable(() -> doSomeReallyLongNetworkRequest())
.subscribe(data -> {
showTheDataOnTheUI(data);
});
Where do you think the doSomeReallyLongNetworkRequest() will run (thread-wise)?
Well if you will run this code at main thread, the network call will run at main thread!
"Observables Are Less Opinionated" means that the multi threading is abstracted away from the actual work. the Subscriber don't know (and don't need to), where the Observable will run, it can run on thread-pool, event loop, or it can even run in blocking fashion.
That's why all Observable interaction happen with async API.
While putting it this way seems like a drawback, the opposite is true, that means that you have greater control where each part of your code is run, without exposing the operation itself and the code that react to Observable emission to this knowledge.
This is done using the Schedulers mechanism in RxJava, together with subscribeOn()/observeOn() operators:
Observable.fromCallable(() -> doSomeReallyLongNetworkRequest())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(data -> {
showTheDataOnTheUI(data);
});
now you told the Observable to perform the subscription code (doSomeReallyLongNetworkRequest()) to run on IO Schdeuler that will create a dedicated thread for the network request, and on the other side, you told the Observable to notify the about emissions (onNext()) Subscriber (showTheDataOnTheUI(data)) at main thread (sorry for android specifics).
With this approach you have very powerful mechanism to determine where and how both operations will work and where notifications will be fired, and very easily ping pong between different threads, this great power comes because of the async API, plus the abstraction of threads away to dedicated operators and Scheduler mechanism.
UPDATE: further explanation:
Client code treats all of its interactions with Observables as
asynchronous
Client code here means any code that interacts with the Observable, the simple example is the Subscriber which is the client of the Observable, as of the composable nature of Observable, you can get an Observable from some API without knowing exactly how its operate so :
Observable.fromCallable(() -> doSomeReallyLongNetworkRequest())
can be encapsulate with some service API as Observable<Data>, and when Subscriber interacts with it, it's happen with async fashion using the Observable events onNext,onError,onComplete.
whether your underlying implementation is blocking or non-blocking and
however you choose to implement it.
"underlying implementation" refers to the operation the Observable do, it can be blocking work like my example of network call, but it can also be a notifications from the UI (clicks events), or update happens at some external module, all of which are non-blocking implementation, and again as of its async API nature the Subscribe shouldn't care, it just need to react to notifications without worrying about where and how the implementation (Observable body) will act.
I am working in WPF now...here we use Background_Worker,TaskFactory.StartNew functions and async/await.
In web apps I know that we have AJAX, MVC partial views etc.
My question is do we really use above mentioned async methods (Tasks, sync/await) in Web apps ? If we use, could you give me some examples ?
I have a document generation module in my web app in which I have to fetch data from DB and create documents out of it. Is it a good scenario for async calls? (to call the function and throw..and never care about it).
I am working in WPF now...here we use Background_Worker,TaskFactory.StartNew functions and async/await.
You really should be using Task.Run instead of BackgroundWorker and Task.Factory.StartNew. Task.Run has much better defaults than StartNew, and is more composable and type-safe than BackgroundWorker.
My question is do we really use above mentioned async methods (Tasks, sync/await) in Web apps ?
On ASP.NET, you should almost never use your own background threads (BackgroundWorker, StartNew, Task.Run). However, you certainly can use async and await.
If we use, could you give me some examples ? I have a document generation module in my web app in which I have to fetch data from DB and create documents out of it.
Yes, database queries are one example of an I/O-based operation. So is "writing" documents - whether as uploads to cloud storage or to the local file system.
I have an MSDN article on async ASP.NET that you may find useful.
Async is useful any time you have disk/database/network I/O. It allows you to do other things while you wait for the I/O to complete, which can result in significant savings. Async isn't really useful for normal CPU bound operations, because of the increased overhead of making it async.
This holds true in WPF as well web applications.
If you want to see a concrete example, Scott Hanselman has a blog entry entitled The Magic of using Asynchronous Methods in ASP.NET 4.5 plus an important gotcha which you may find very interesting.
Actually my question is not only targeting for play framework, but a general question for async programming.
I know that in play, we can use:
WS.url(url).get()
to make a rest call async by return something like promise/future. (I guess it might be supported by underlying NIO library like netty or spray.io)
So, my question is: to build a fully async web application, every I/O bound operation has better to be async, right? Otherwise, it will be a bottle neck?
Let's say I have a very simple web application which only calls a JDBC and return query value to browser.
In this case, suppose I don't have an async/reactive JDBC driver, will play2 performs better than a servlet solution?