9 Replies Latest reply on Oct 19, 2010 8:27 PM by ming chen

    rich:fileUpload listener is jumping out of the conversation

    Bogdan Minciu Newbie

      Hello guys and girls,


      I'm using RichFaces 3.2.1-SNAPSHOT, Seam 2.0.1-GA and Facelets.


      I try to use the rich:fileUpload component to upload 5 pictures at once. This is from my editImageSet.xhtml page:


      <rich:fileUpload fileUploadListener="#{imageSetView.imageUploadListener}"
                                               maxFilesQuantity="5"
                                               id="upload"
                                               acceptedTypes="jpg, jpeg, gif, png">
                                  <a:support event="onuploadcomplete" reRender="currentImageSetA, messagesA" />
                              </rich:fileUpload>
      



      The editImageSet.xhtml page is called like this: <s:link action="#{imageSetView.editImageSet(imageSet)}" view="editImageSet.xhtml" value="edit"/>, so it starts a new CONVERSATION when entering the page.


      And this is my listener:


      @Stateful
      @AutoCreate
      @Name("imageSetView")
      @Scope(ScopeType.CONVERSATION)
      public class ImageSetViewBean implements LocalImageSetView {
      ...
      
          @In
          private EntityManager entityManager;
      
          private ImageSet currentImageSet;
      
          @Begin(join = true)
          public void editImageSet(ImageSet selectedImageSet) {
              setCurrentImageSet(imageSetDAO.findImageSetById( selectedImageSet.getId() ));
              
              setImages(currentImageSet.getImages());
              log.info("images= #0", getImages());
          }
      
          // also tried without the @Begin
          // also tried with @Begin(join = true)
          @Begin(nested = true)
          public void imageUploadListener(org.richfaces.event.UploadEvent event) throws IOException {
              if (event == null) {
                  log.warn("imageUploadListener(): null upload event");
                  return;
              }
              
              org.richfaces.model.UploadItem item = event.getUploadItem();
      
              if(item.isFile()) {
                  java.io.File tempFile = item.getFile();
                  fileName = tempFile.getAbsolutePath();
                  
      // ---
      // --- some image copying, resizing that was removed from this forum post ---
      // ---
      
                  try {
                      Image db_main = new Image();
                      db_main.setUrl(fileName);
      
                      log.info("em= #0, db_main= #1", entityManager, db_main);
                      entityManager.persist(db_main);
                      
                      currentImageSet.getImages().add(db_main);
                      entityManager.merge(currentImageSet);
                      
                  } catch(Throwable t) {
                      t.printStackTrace();
                      return;
                  }
                  
              }
              
              log.info("Image uploaded.");
          }
      ...
      }
      



      Well, the problem is when it comes to the entityManager.persist(db_main) line. It jumps out to the /home.xhtml page saying that The conversation ended, timed out or was processing another request. If I comment the lines that deal with the database (persist, merge) everything works well, all 5 files are uploaded, resized, moved.


      The interesting thing is that the first file (image) uploaded is saved on disk, and even persisted. And it is even added to the currentImageSet. So the method works well just for the first file.


      I investigated this using the Seam Debug Page, and I have seen that the conversation was still active, but somehow, the imageUploadListener() jumped out of it, even if it is annotated with @Begin(join=true).


      Could anyone please tell me why is the imageUploadListener() method jumping out of the conversation if it calls the entityManager?


      Thank you in advance for any hint,


      Bogdan.

        • 1. Re: rich:fileUpload listener is jumping out of the conversation
          Zahid Maqbool Newbie

          Hi Bogdan,


          I was also facing a similiar problem like yours. I used to get messages that conversation timed our or ended.


          I fixed this problem by changing the concurrent-request-timeout in components.xml


          <core:manager concurrent-request-timeout="500"
                           conversation-timeout="120000"
                           conversation-id-parameter="cid"/>

          to



          <core:manager concurrent-request-timeout="5000"
                           conversation-timeout="180000"
                           conversation-id-parameter="cid"/>



          After this I solved my problem... Please let me know if your problem is fixed or not after this.


          • 2. Re: rich:fileUpload listener is jumping out of the conversation
            Bogdan Minciu Newbie

            Thank you Zahid!


            Your post really helped me with this problem. Although I don't understand what I just modified, I will take a closer look on that concurrent-request-timeout to see what are its implications.


            I will test this on a real remote webserver, and I will keep you updated if there are problems with file uploads outside localhost.


            Thank you,
            Bogdan.

            • 3. Re: rich:fileUpload listener is jumping out of the conversation
              Zahid Maqbool Newbie

              If you find anything interesting please let me know about it

              • 4. Re: rich:fileUpload listener is jumping out of the conversation
                Miha Majcen Newbie

                Hi!


                I was also facing a similiar problem. I change
                concurrent-request-timeout and conversation-timeout.
                Now are files uploaded, but in upload time I get few new Converstions, bigger is File, more Converstion I get.


                Java code:



                public boolean uploadFile() {
                 try {
                  getInstance().setKoncnica(fileName.substring(fileName.lastIndexOf(".")+1, fileName.length()));
                  getInstance().setFilename(getInstance().getSifra()+"."+getInstance().getKoncnica());  
                  String path = IMAGE_PATH + "/" + getInstance().getFilename();
                  File file = new File(path);
                  FileUtils.writeByteArrayToFile(file, this.fileData);
                  info("UploadFinished");
                 } catch (Exception e) {
                  error(e.getMessage());                            
                  FacesMessages.instance().addFromResourceBundle(FacesMessage.SEVERITY_ERROR,  "image_uploading_error");
                  return false;
                 }
                 return true;
                }
                
                public void uploadListener(UploadEvent event) throws IOException{
                 UploadItem item = event.getUploadItem();;
                 if (item.isTempFile()) {
                  this.fileName = item.getFileName();
                  if (item.getFile() != null ) {
                    this.fileData = FileUtils.readFileToByteArray(item.getFile());
                    uploadFile();
                    return;
                  } 
                 } 
                 FacesMessages.instance().addFromResourceBundle(FacesMessage.SEVERITY_ERROR,   "image_uploading_error");
                }



                Regards,


                • 5. Re: rich:fileUpload listener is jumping out of the conversation
                  Klaasjan te Voortwis Newbie

                  I ran into the same issue when trying to run a long-running process on the uploaded file.


                  My solution: in the listener, call an @Asynchronous method to process the file. Since I changed this, the issue has gone.



                    public void listener(UploadEvent event) {
                      UploadItem item = event.getUploadItem();
                      log.info("File : '" + item.getFileName() + "' was uploaded");
                      processGmlFile(new Date(), item);
                    }
                  
                    @Asynchronous
                    public QuartzTriggerHandle processGmlFile(@Expiration Date when, UploadItem item) {
                      //processing here
                    }
                  



                  Regards, Klaasjan

                  • 6. Re: rich:fileUpload listener is jumping out of the conversation
                    Yasser Hawari Newbie

                    I also used Klassjan way to handle this issue. the point is, concurrent time out will not work if ur file is large - by large i mean, it takes longer to upload than the concurrent-request-timeout.

                    • 7. Re: rich:fileUpload listener is jumping out of the conversation
                      Frank Pfleger Newbie

                      I recently ran into the same problem.
                      I think Klassjan way won't work on large files, just on long running processing of the uploaded file.
                      Any other solutions for this problem?

                      • 8. Re: rich:fileUpload listener is jumping out of the conversation
                        Frank Pfleger Newbie

                        After hours of try and error I finally found a good solution...
                        I'm using a Seam Phase Listener (RESTOREVIEW, @Observer(org.jboss.seam.afterPhase)) where I check if the current Conversation is locked.
                        If so, I'll just wait until it's unlocked again or ended
                        This way I lose the percentage upload status, but it'll work for every filesize!

                        • 9. Re: rich:fileUpload listener is jumping out of the conversation
                          ming chen Newbie

                          This is my workaround of this issue.
                          I encountered the same problem that listener jumps out of conversation scope. One of the senior devs in the company suggested adding
                               <page view-id="/random_directory/fileupload.xhtml">
                                    <begin-conversation join="true"/>

                          in pagex.xml, fileupload.xhtml being the page where the rich:fileupload is in.
                          It worked fine for me. Now the listener is confined to the conversation scope now.