JavaFX : How to process a webview in a non application thread - node.js

I have a JavaFX application that contains a WebView among other JavaFx views in the same window. The WebView opens a URL to a NodeJs webapp that consumes a lot of CPU resources.
With this resource consumption from the WebView, the other JavaFX views are working slowly.
For our application, we have a very powerful system with 12 virtual threads in the processor.
So, what I need is to deport the WebView processing to another thread so that it won't affect the behavior of the other JavaFX views. Is there any way to achieve this?

You cannot do this. JavaFX has a single application thread per JVM process (java invocation) and WebView API calls must be processed on the JavaFX application thread.
Note that internally, the WebView uses WebKit which may have its own threading implementation to support html5 stuff like Web Workers, but that is all hidden from you when you are programming WebView at the Java API level. So that won't make much of a difference unless you explicitly program your JavaScript to make use of it. I guess if your NodeJS webapp is optimized for other browsers it will probably work fine in WebView, you will just need to benchmark, which I guess you did and found something wanting. You may want to expend some effort optimizing the NodeJS webapp that you have.
The only way to achieve something similar to what you are requesting in your question is to launch a separate process, i.e., a new JavaFX application in a new JVM process, that contains your WebView instance.

Related

Is threading is necessary to use in SQLite based Delphi app?

I am very much beginner in Delphi development. I want to make desktop application that include Sqlite database and browser. The application is mostly based on CRUD operations but when ever user update data into database the change will be displayed on browser on another panel of the application. it also generates printable pdf. The pdf is generated by C# code[NReco Library]. Is I have to use Multi Threading in to my application to make my ui responsive in this case.
SQlite3 has very low response time. A simple SELECT is less than a few microseconds in practice, since it is an embedded database, and no network layer is involved.
No need to implement background threading with SQLite3.
For demanding content, e.g. reading a lot of data, you could:
Use proper paging (e.g. read 10K rows each time);
Call Application.ProcessMessage every now and then;
Add a timeout in the loop.
You should design your app just as if it was a client app. Too much data is killing the data, both for the UI and the User. Only get what you need to for proper display.
It's probably not necessary, but it is always a good idea to run long running tasks on a separate thread. Delphi makes that really easy so it should be of no concern.
Alternatively, you can use techniques like periodically calling Application.ProcessMessages() to keep your UI responsive.

Can I run a Java FX and a Swing UI in the same process?

I have bigger multi-threaded UI application with a lot of background calculations&io tasks. There is a new component coming up with an UI I'd like to write in javafx.
Now my question is, can I run a swing UI on the AWT-Dispatcher Thread and a Javafx UI on the FX Application Thread in the same process or does this have any bad consequences?
Thanks for any answer!
You can. It's supposed to make the migration from Swing to JavaFX easier. However, you need to consider the threading as you guessed.
Here's an article with more information about it: Integrating JavaFX into Swing Applications

Run RIA service on non UI thread

I am trying to make RIA service calls from non UI thread.
I made calls with opening new thread and with background workers, but for both cases callback is running on UI thread.
Is it possible to execute callback on callers thread, not UI?
Thanks
tl;dr
Use WCF
Marshal your results to the UI thread yourself
WCF RIA is built to do work on the UI thread
WCF RIA was designed to work mostly on the UI thread. That obviously has many pros and cons; mostly cons in your case. I'm having trouble finding definitive documentation of this design, however, most questions on the topic are answered by affirming the UI threadedness.
Here is a post from a member of the WCF RIA QA team indicating UI-thread-only processing.
Andy French describes the callbacks marshalling to the UI thread:
The Domain Context Load and SubmitChanges execute asynchronously. They take a thread from the thread pool, make the necessary calls to the server, and when those calls complete the work is automatically marshalled back to the UI thread to modify the entity collections and subsequently update the UI (probably via INotificationChanged).
If you use WCF to get your own data, you can do it on any thread you like. When the calls complete, you will have to write (or borrow) the code to update the UI on the UI thread to avoid cross thread exceptions.
IMO, the main advantages of WCF RIA giving simple problems simple solutions:
Great tooling for re-using code between the server and client
Service and client code are always compatible
Transferring data to/from the client/server is relatively simple
WCF RIA is strongly opinionated resulting in easy-to-learn coding patterns
The cons make hard problems hard or impossible:
WCF RIA is strongly opinionated and not following that opinion is painful or impossible
All operations return on the UI thread, often causing performance problems
There is some voodoo to achieve the highest amount of client+server code re-use

Implementing concurrency in Java EE Web application

We are creating a web app where we need to have concurrency for a few business cases. This application would be deployed in a tomcat container. I know that creating user defined threads in the web container is a bad idea and am trying to explore options that i have.
Have my multi-threaded library used as a JCA component. We are averse to using this approach because of the learning curve that might be involved.
I know that there's WorkManager API's available but i guess thats not implemented by tomcat so this option goes out.
I did some research and found out that CommonJ library is recommended for Tomcat. Has anyone used it?
Also, I see that there are ManagedExecutorService available but I am not sure how to use it and is it different from WorkManager API's (and the commonJ library)?
Any help on this appreciated. By the way, using JMS is out of question because of deployment environment. I am inclining towards points 3 and 4 but i do not have much knowledge on it. Could someone guide pls.
Since you're using Tomcat, don't worry about it and do whatever you want. The Servlet section of Java EE makes no mention of threads etc. That's mostly under the EJB section.
Tomcat itself doesn't do much at all in terms of worrying about managing threads, it's a pretty non-invasive container.
Its best to tie your threads to a ServletContextListener so that you can pay attention to the application lifecycle, and shutdown your stuff when you app shuts down, but beyond that, don't overly concern yourself about it and use whatever you're happy with.
Addenda -
The simple truth is Tomcat does not care, and it's not that sophisticated. Tomcat has a thread pool for each of the HTTP listeners and that's about the end of its level of management. Tomcat is not going to take threads from a quiet HTTP listener and dedicate them to a busy one, for example. If Tomcat was truly interested in how you create threads, it would prevent you from doing so -- and it doesn't.
That means that thread management outside of the HTTP context falls squarely on your shoulders as an implementor. Java EE exposes these kinds of facilities, and the interfaces make great reads. But the simple truth is that the theoretical capabilities espoused by the Java EE API docs, and the reality of modern implementations is far different, particularly on low end systems such as Tomcat.
Not to disparage Tomcat. Tomcat is a great piece of software. But for most of its use cases, the extra management capability simply is not necessary.
Setting up your own thread pool (using the JDK provided facilities) and working with your own thread lifecycle model will likely see you successfully through whatever project you're working on. It's really not a big deal.
There are a couple of options. Regardless container restrictions that might or might not be in place, spawning individual threads on demand is nearly always a bad idea. It's not that this wouldn't work in a Servlet environment, but the number of threads you can potentially create might get completely out of hand.
The simplest solution to go with is a plain old Java SE thread pool via a normal executer service. Start the pool in a Servlet listener and provide access to it via some static variable. Not overly pretty, but it gets the job done. Depending on your exact use case this might actually be the best solution (if your use case is pretty low-level).
Another option is to add OpenEJB to your war, and then take advantage of the #Asynchronous annotation.
Yet another option, is to realize that one typically uses Tomcat if the business requirements are extremely simple or low-level. That's pretty much the entire point of using something as bare bone a Tomcat. As soon as you find yourself in need of adding (tons of) libraries, you might have outgrown Tomcat and might be better of using a server that already has the functionality you need (in this case asynchronous execution). Examples are TomEE, GlassFish, Resin, JBoss AS, Geronimo, etc.
Every Servlet -Java EE base component for HTTP request processing- in your Web Application is a Singleton, and each request runs in its own independent thread so there is no need to start/stop user generated threads on your own. Your Web Container -in this case Tomcat- manages all that stuff.
Besides that, you need to have in mind some considerations for multi-threaded processing in your code. For example, since Servlets are singletons and many threads are spawned for this class is a bad idea to have instance attributes in this components.
I have used CommonJ many times and it works very well. It can be initialized and destroyed from a ServletContextListener.

Multi-thread support in IPad, IPhone

Just to confirm if multithreading is supported in iPad and iPhone. I need to write an application that calls a web service on a background thread while user can still interact with the UI.
Yes, it has threads.
Also, you don't need to spawn your own threads for most tasks. A lot of the Apple Frameworks (Cocoa Touch Foundation frameworks) have built-in code that will do this for you.
Also, if you're consuming data from a Web API (HTTP Request), I highly recommend you take a look at ASIHTTPRequest, which is built on top of NSOperation (an operation class that easily handles background processing).
Finally, if you need help on program design/flow using the Model-View-Controller pattern, I've written a blog entry describing how to consume Web API data and present it to the end-user in a table format.
iOS has always supported multi-threading, just as Mac OS X did before it. Here's Apple's excellent overview documentation on the subject:
Concurrency Programming Guide

Resources