4 Replies Latest reply on Aug 22, 2011 6:31 AM by kem

    How to display server side logging in the UI

    kem

      Hi,

       

      I have a long running (>1h) server side process generating a useful logging information. How to display these log info to the UI.

      I am using seam2 + Richfaces.

       

      Thanks,

       

      khalil

        • 1. Re: How to display server side logging in the UI
          mp911de

          Hi Khalil,

          there are multiple approaches to achive that.

          You need to write your output in some model, file or database, to keep it available for your user. Then, you could use a inputTextArea, an outputText, a richDataTable or something to create your output layer. And i think, the best way to update it would be a Ajax-Poller, which fires an event, on this event you pull the latest log entries, put it to your UI model and you do a rerender on the outputPanel where you have your textarea/outputText or table.

           

          The process you are going to start needs to be async in some way (thread, native OS process or sth.), else you won't get a smoot app.

           

          Tell me, what you think about it.

           

          Best regards,

          Mark

          • 2. Re: How to display server side logging in the UI
            kem

            Thanks Mark,

             

            it's a complex approach vs my skills ;-). I am looking for something more simple. There must be something to inform users about long-running server side processes. I mean something simple.

             

            Regards,

             

            khalil

            • 3. Re: How to display server side logging in the UI
              mp911de

              Hi Khalil, take a look on following code. This should be a easy version (I've used JSF2.0 and RichFaces4 for this). I also attached my project (you can use it with eclipse and/or maven):

               

               

              Controller.java

               

              /**

              * Controller Bean.

              * Created: 22.08.2011 <br>

              * <br>

              */

              @ManagedBean

              @SessionScoped

              public class Controller

              {

               

                        private Model model = new Model();

               

                        public Model getModel()

                        {

                return model;

                        }

               

                        public void startLongRunningTask()

                        {

                // Beware of this in real J2EE envs as Threads are a bit risky. Use EJB Timers od JMS instead.

                                  LongRunningTask task = new LongRunningTask(model);

                                  task.start();

               

                        }

               

              }

               

              Model.java:

              /**

              * Model Bean.

              * Created: 22.08.2011 <br>

              * <br>

              */

              public class Model

              {

               

                private boolean running = false;

                private boolean success = false;

                        private List<String> messages = new ArrayList<String>();

               

                /**

                         * @return the running

                         */

                        public boolean isRunning()

                        {

               

                return running;

                        }

               

                /**

                         * @param running

                         *            the running to set

                         */

                        public void setRunning(boolean running)

                        {

               

                                  this.running = running;

                        }

               

                        /**

                         * @return the success

                         */

                        public boolean isSuccess()

                        {

               

                           return success;

                        }

               

                /**

                         * @param success

                         *            the success to set

                         */

                        public void setSuccess(boolean success)

                        {

               

                                  this.success = success;

                        }

               

                /**

                         * @return the messages

                         */

                        public List<String> getMessages()

                        {

               

                synchronized (messages)

                                  {

                                            List<String> copy = new ArrayList<String>();

                                            copy.addAll(messages);

                                            return copy;

                                  }

                        }

               

                /**

                         * @return the last Message.

                         */

                        public String getLastMessage()

                        {

               

                synchronized (messages)

                                  {

                                            if (messages.isEmpty())

                                            {

                                                      return "";

                                            }

                                            return messages.get(messages.size() - 1);

                                  }

               

                        }

               

                        public void addMessage(String message)

                        {

               

                synchronized (messages)

                                  {

                                            messages.add(message);

                                  }

                        }

               

              }

               

              LongRunningTask.java:

              public class LongRunningTask extends Thread

              {

               

                        private Model model;

               

                /**

                         * @param model

                         */

                        public LongRunningTask(Model model)

                        {

               

                                  this.model = model;

                        }

               

                /**

                         * @see java.lang.Thread#run()

                         */

                @Override

                public void run()

                        {

                try

                                  {

                                            model.setRunning(true);

               

                                            Thread.sleep(2000);

                                            model.addMessage("first");

                                            Thread.sleep(2000);

                                            model.addMessage("second");

                                            Thread.sleep(2000);

                                            model.addMessage("third");

                                            Thread.sleep(2000);

                                            model.addMessage("last");

                                            model.setSuccess(true);

                                  }

                                  catch (Exception e)

                                  {

                                            model.setSuccess(false);

                                  }

                finally

                                  {

                                            model.setRunning(false);

                                  }

                        }

              }

               

              index.xhtml:

              1 of 1 people found this helpful
              • 4. Re: How to display server side logging in the UI
                kem

                Many thanks Mark,

                 

                I am using seam/J2EE. Many components are injected. I am using asynch (ejb timer or quartz) call to the long running process method. But In this env, I can't inject any components!!!

                 

                hope I can find some doc about this issue.

                 

                Regards,

                khalil