Hibernate Queries in GWT - java

I wanted to fire HQL queries from GWT client. But As it comes asynchronously, If I want to fire a sequence of queries, How do I do that?
Asynchronously all the data is transferred to client and used. But If I want to fire the queries in sequence how do I do that?
Mainly, the order of processing those HQL results should not be changed.
Let me know if you have any queries. Thanks in advance.

GWT Client will fire your queries in the order you have defined in client file i.e
yourClient.java
Query q1; //execute method first request
Query q2; // execute method second request
The Queries will run on sequence first q1 then q2 but q2 will not wait for the completion of q1. Once request send you can not assure about the order inwhich you will get response it might be possible that q2 will execute and return the response first while q1 is in progress.
Mostly operations for making sequence from client side is bit expensive. i.e you can use the queries on success method of AsyncCallback. When response of q1 will comes then q2 will be executed but if you have several queries then its an expensive solution.
So best thing is that make a Service method execute your all queries at server side and return map of result then populate it in your client side in any sequence you want to show the data.

Using Hibernate with GWT is not as simple as you may think. Google has a good documentation for the use of Hibernate. Did you already read that documentation?
I can recommend the use of Gilead. I'm not sure if this would solve your problem because I'm not exactly sure what your problem is.

GWT is asynchronous so you have to build everything based on request->callback
You can do two things in a case like this:
Have only a single RPC service that gathers the results for all queries and returns them. Remember that the services are plain java servlets
Have many RPC services that do one query each. Make the query to the first and in the callback you can display (or gather) whatever result and invoke the second query and in its callback do the same procedure... and so on.

Related

Is it possible to update subscription message using previous state?

Suppose I have this subscription query like this:
queryGateway.subscriptionQuery(
FetchListOfBookQuery,
ResponseTypes.multipleInstancesOf(Book::class.java),
ResponseTypes.multipleInstancesOf(Book::class.java)
)
So, it will subscribe to list of the Books in databsae and If I want to add a new book I would have something like this in my projection:
fun on(event: BookAddedEvent){
var book = repo.save(Book(event.bookId)).block()
queryUpdateEmitter.emit(
FetchListOfBookQuery::class.java,
{ it.bookId == book.bookId },
book
)
}
The problem is, since I only got one instance of a new Book which has been added, in order to update to the subscription query I need to have previous list of Books as well. Is there a way to get the previous update state of the subscription query and compare changes and finally update it?
The Subscription Query logic provided by Axon Framework allows you to retrieve an initial response and updates. In code, this translates itself to firstly hitting an #QueryHandler annotated method and secondly emitting the updates through the QueryUpdateEmitter.
What is being emitted is completely up to you. So if you decide to send the newly added Book in combination with all the previous Books, that is perfectly fine. As you have likely noticed though, the QueryUpdateEmitter does not store the updates itself, neither does the SubscriptionQueryResult on the query dispatching end.
Thus if you need logic to filter out what has been send with a previous update, you will have to build this yourself. To that end you could take the route of building a dedicated piece of logic, a service maybe, which does the job. Or, you could create your own QueryUpdateEmitter which enhances the behaviour to simplify the update being send.
I'd argue the latter would be the cleanest approach, for which I'd recommend wrapping the SimpleQueryUpdateEmitter. However, this could be quite some custom code, so I'd first check whether there is a different way around this requirement you are stating:
... but in order to update to the subscription query I need to have previous list of the books.
If you do end up on that route through bare necessity, I would be interested to see the outcome, or potentially help out with suggestions on the matter.
That's my two cents, hope this helps you out #Patrick!

How to consume yield results fetched one by one by database on demand from an API end point? Are web sockets needed?

I am thinking of setting up a page in an application that each of the queries can return a resultset that cannot fit in memory or the query is very expensive to fetch all of them. The user will be hitting "get more" to get more of those results. I wonder if I could use a yielder for Java something like that (http://benjiweber.co.uk/blog/2015/03/21/yield-return-in-java/) and if I will need Web Sockets e.g from Spring (http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html) so that the client can tell to Server to push more results. Also could you please give an example of the handshake .. Will the endpoint uri be based on some session id as well? Also when databases like OrientDB/Neo4j return Iterables does it mean that we can keep the connection open and get the next rows after minutes without problems? Thanks!
You are talking about two different concepts.
Pagination
If you have a large result set and you need to return it piece by piece to avoid long query times or high memory requirements, you're paginating the over the result set.
To do this, you require another piece of the set hitting "Get More" button from the client. Each time you require more, the server will receive a request from the server and will hit the DB with some paginated query.
Example in SQL (page 10, 10 results/page , for instance):
SELECT * FROM Table OFFSET 100 LIMIT 109
Websockets / Yielder
You'll need a websocket / yielder when is the server the one who sends data, in other words, the client doesn't require an update, it only keeps the socket open and receives updates from the Server when they come.
That's the case of a Message service, for example, avoiding constant polling from the client side.
In your case is absolutely unnecessary a websocket. You can also see an example of what I'm saying here -> What's the behavioral difference between HTTP Stay-Alive and Websockets?
However you can setup a keep-alive connection between your back-end and database in order to avoid closing/opening constantly the connection each time the user requires more results.
Finally, your question about Iterable results in Neo4j. Neo4j's result type is an Iterable list of Map<String,Object> which represents a List of key-value pairs. That doesn't keep the connection alive (by default), it only iterates through the returned results of that particular query.

How to gracefully fire many request to an API

I have been given a API link of the form of a URL and query string. And following is my approach,
Query string format means that a GET request is to be fired.
I also assume that this can be done with the HttpURLConnection in Java
I have some data list that I'm retrieving from db
How would I fire for each data in list? Is a simple for loop not going to be enough for such a sophisticated task?
The API link is a trivial link with query string with data from db to be appended to one at a time.
Would like to hear how you would approach this task and see if my approach lacks somewhere.
You are right in doubting the simple for loop approach. It would be slow. The request is blocking, so you'll be waiting for the result of request 1 before firing request 2. Look into doing this asynchronously, firing multiple requests at once.
It's hard to say more without details on the API. Is it an online web service? Something internal created by another department? If it does not exist, consider asking for a version of that function that can receive multiple parameters at once, instead of having to do tons of tiny calls.

Pipelining web service response

I am designing a web service that wraps a very large data source and I would be very grateful for any suggestions whether my design is appropriate or I am missing something substantially better.
So here is the problem:
We have several data sources that all provide the same interface with the "most important" method being RowIterator select(Table table, String where). Now, functionally everything is going fine for all our implementations but the problem is that the web service that we need to wrap around one of the sources would (in a naive implementation) upon receiving a query
wait for the wrapped data source to return the whole result set
marshal the whole result set before sending it to the client
at the client side unmarshal the whole result set before returning it to the caller
Only after this sequence would the caller be able to see the first row. This is a quite disappointing behavior as the caller has to wait unnecessessarily for the whole result set twice. I want to have some pipelining, instead. The caller must be able to see the first results while the service is still sending rows. Now I am planning to overcome this by implementing some kind of paging that is encapsulated in my client-side row iterator. The service would maintain a session id (with a timeout) that is created upon receiving a query and can be used to fetch chunks of data. The session id could already be returned before sending the actual query to the wrapped data source. The client would then fetch chunks (pages) until a chunk is empty or smaller than the expected (= requested) chunk size.
So, in this design the caller would be able to see the first results while the service is still sending rows. However, I am wondering whether there is a way to efficiently pipeline results on a per-row basis using a SOAP web service?
Also, would it be possible to return the results to the caller without repeatedly asking for more results?
In the end I used MTOM to transmit the data in binary and used blocking queues at the client and the server to achieve the desired parallelism. I sketched this here: Streaming large SOAP attachments

How do I asynchronously get data from the database for a SwingWorker's publish method?

I have a remote EJB3 method that returns a List<T> from the DB using JTA (Hibernate).
Basicly it's just a simple HQL that looks something like this: select t from T t where ....
This query takes a long time in some cases so I wanted to use a SwingWorker to asynchronously load chunks of the data into a JTable using the publish and process methods.
But, for that to work I also need to get the data in chunks from the server. right?
I can artificially divide the query into a number of "smaller" queries by setting some arbitrary range to them:
select t from T t where ... and t.id < :rangeSrart and t.id > :rangeEnd
But I was hoping there is a better way of doing that.
I looked at #Asynchronous but it doesn't seem to be what I'm looking for.
Is there any way to do this with EJBs or anything else?
You need neither publish nor process. Simply get the list in your implementation of doInBackground(). Before start of execute() show an undetermined progress bar in a dialog. Also you should override done() to show your results and hide progress dialog.

Categories

Resources