in Education by
I have a FileCreator class that implements StreamResourceWriter interface and MainErrorHandler class that implements ErrorHandler. I'm using the MainErrorHandler class as a centralized Exception handler in my project which mostly logs the exception and shows a notification to the user. The problem is that StreamResourceWriter.accept() method runs in a non UI thread and when an Exception is thrown it is directed to the ErrorHandler which then fails to show a notification due to "IllegalStateException: UI instance is not available". Is there a way to show a notification window to the user from MainErrorHandler when FileCreator throws an error in accept() method? Below FileCreator snippet. public class FileCreator implements StreamResourceWriter { @Override public void accept(OutputStream stream, VaadinSession session) throws IOException { // Run in a non ui thread. // Writes to OutputStream but an Exception might be thrown during this process } } Below MainErrorHandler snippet. /** * Centralized error handler */ public class MainErrorHandler implements ErrorHandler { private static final Logger log = LoggerFactory.getLogger(MainErrorHandler.class); @Override public void error(ErrorEvent event) { log.error("Error occurred", event.getThrowable()); //Cannot show a notification if ErrorEvent came from FileCreator. //Will get an IllegalStateException: UI instance is not available. Notification.show("Error occurred"); //Tried UI.getCurrent but it returns null if ErrorEvent came from FileCreator. UI.getCurrent(); } } Using Vaadin 13.0.1. Edit One way to solve this issue is to pass UI reference to FileCreator directly. Below an example. public class FileCreator implements StreamResourceWriter { private UI ui; //Pass UI reference directly public FileCreator(UI ui){ this.ui = ui; } @Override public void accept(OutputStream stream, VaadinSession session) throws IOException { try{ // Run in a non ui thread. // Writes to OutputStream but an Exception might be thrown during this process }catch(Exception e){ //I don't like this since have to catch all exceptions and have to call ErrorHandeler directly with a UI reference. Also what if somewhere in code ErrorHandler is changed and is not of type MainErrorHandler. ((MainErrorHandler)VaadinSession.getCurrent().getErrorHandler()).error(e, ui); } } } As I said in comments I really don't like this approach either since I am forced to catch all Exceptions, have to cast ErrorHandler to MainErrorHandler and calling it directly. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
There is a way, but it's not perfect. You can get all UI instances via VaadinSession.getCurrent().getUIs(). To filter out the inactive/detached UIs you can check if ui.getSession() returns a VaadinSession (so, not null). The JavaDoc of getSession says: The method will return null if the UI is not currently attached to a VaadinSession. Then you can invoke the access method on each of the UIs and create and show the notification inside the UI-context. for(UI ui : VaadinSession.getCurrent().getUIs()) { // Filtering out detached/inactive UIs if (ui.getSession() != null) { ui.access(() -> { // create Notification here }); } I said it's not perfect because you have to keep in mind that the user can have several UIs opened at the same time(e.g. multiple tabs).

Related questions

0 votes
    There is an SQL interface for relational databases via the _______ package. (a) DIB (b) DB2 (c) DBI (d ... Operations of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    Any easy way to move and custom AMI image between regions? (North Virginia -> Singapore) I know you can mess up with ... way to do it? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    Which of the interface contains all the methods used for handling thread related operations in Java? (a) ... questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    From my silverlight 4.0 application. I can access the WCF File easily but when moved to https, I ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
0 votes
    The question as the title. Why is this, I have used the ssh command: ssh -i mykey.pem root@xxx-xxx-xx-xx-xxx ... What am I doing wrong? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    What are the methods of handling an Error in MVC?...
asked Apr 5, 2021 in Education by JackTerrance
0 votes
    Explain the error handling in Talend....
asked Mar 18, 2021 in Technology by JackTerrance
0 votes
    List of all available Fiori apps can be explored via _________. A. SAP Fiori apps library B. SAP Fiori apps list C. SAP Fiori apps engine...
asked Mar 2, 2023 in Technology by JackTerrance
0 votes
    I have a fragment with a view and an options menu: public class OfferingsFragment extends Fragment { public ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I have a fragment with a view and an options menu: public class OfferingsFragment extends Fragment { public ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 23, 2022 in Education by JackTerrance
0 votes
    I have the access to this DOM node in temp1.$el. And here is the content which the above variable ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 26, 2022 in Education by JackTerrance
0 votes
    I have the access to this DOM node in temp1.$el. And here is the content which the above variable ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 14, 2022 in Education by JackTerrance
0 votes
    I have the access to this DOM node in temp1.$el. And here is the content which the above variable ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    I do not want to retrieve the review's "state" ( e.g., 'open', 'closed' ) but rather ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    My task is connected to the container via ssh from tests. I have dockefile: (almost from https:// ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
...