1 Reply Latest reply on Jan 20, 2009 4:48 AM by norman

    Using Seam from within a servlet

    waltc

      Hi,
         I am trying to write a Seam application that has a long lived nature. While I can get a page to refresh periodically I want the database to grow even if the web page is not being run, i.e. the action is not getting executed. So I created a servlet which is loaded at startup after Seam and am injecting the Logger, the EntityManager, and two entities. By doing this the actual web app need only process the existing tables. However log and em are never getting injected. I do ensure this servlet is not loaded until seam is loaded. Is there anything obvious to you guys.


      Thanks,


      Walt


      Here is what I have



      |public class IndexMonitor extends HttpServlet {
           /**
            * 
            */
           @In private EntityManager em;
           @Logger private Log log;
           private static final long serialVersionUID = 1539127472671034423L;
           private final ScheduledExecutorService scheduler = 
                  Executors.newScheduledThreadPool(1);
           private String indexXMLFile;
           private File indexFile;
           private long indexModDate;
           private List<ConnectorInformation>connectorList;
           private String contextPath;
           public void init(ServletConfig config) throws ServletException {
                contextPath = config.getServletContext().getContextPath();
                contextPath = config.getServletContext().getRealPath(contextPath);
                String fileSeparator = System.getProperty("file.separator");
                int index = contextPath.lastIndexOf(fileSeparator);
                contextPath = contextPath.substring(0, index + 1) + "WEB-INF" + fileSeparator;
                indexXMLFile = config.getInitParameter("indexconf");
                indexFile = new File(contextPath + indexXMLFile + ".xml");
                indexModDate = indexFile.lastModified();
                
                /*
                 * create scheduled Thread pool
                 */
                getServerList();
                for (ConnectorInformation connector : connectorList) {
                     scheduler.scheduleWithFixedDelay(connector, 5, 60, TimeUnit.SECONDS);
                }
                
           }
      
           void getServerList() {
                List<Server> serverList;
                if (connectorList == null) {
                     ServerFactory factory = new ServerFactory();
                     factory.setServerFile(indexFile);
                     serverList = factory.getServers();
                     connectorList = new ArrayList<ConnectorInformation>();
                     for (Server server : serverList) {
                          try {
                               ConnectorInformation connector = new ConnectorInformation(
                                         server);
                               connectorList.add(connector);
                               connector.setEm(em);
                               connector.setLog(log);
                          } catch (Exception e) {
                               if (log != null) {
                                    log.info(e.getLocalizedMessage());
                               }
                          }
                     }
                }
                return;
           }
      |



      Another class


           
           private final String query = "select s from Server s where s.name like ";
      
                if (serverEntity == null) {
                          if (em != null) {
                               serverEntity =  (com.search.model.Server)em.createQuery(query
                                         + getServer().getName()).getSingleResult();
                          }
                     }
                     if (tick == null) {
                          tick = TickData.class.newInstance();
                     }
                     tick.setServer(serverEntity);
                     tick.setId(0);
      ...
                               if (serverEntity != null && serverEntity.getHeapMax() != (max/1024)) {
                                    serverEntity.setHeapMax((int)(max / 1024));
                                    em.persist(serverEntity);
                               }
                          }
                     }
                     if (em != null) {
                          em.persist(tick);
                     }
                     close();
                } catch (Exception e) {
                     if (log != null) {
                          log.error((Object)null, e, (Object)null);
                     }
                }
                
           }
      



        • 1. Re: Using Seam from within a servlet
          norman

          You might take a look at something like ContextualHttpServletRequest to see how it manages the seam contexts from a few of the Seam servlets.  You aren't in a request in your example, but it may give you some ideas.