3

我有一个 ServletContextListener 在我的 JBOSS 7.1 服务器启动时运行。这实质上是在一个文件夹上监听并等待新文件,并在当前等待新的 excel 文件并重命名它们。

爪哇

public class StagedFolderListener implements ServletContextListener {

    @Inject
    TableDao tableDao;

@Inject
ImportDatabase import;

public void contextInitialized(ServletContextEvent e) {
    System.out.println("Listener starting...");
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() 
        {
            ProcessData();
        }
    }, 0, 10000);

}

public void contextDestroyed(ServletContextEvent e) {
    System.out.println("Listener destroyed...");
}

public void ProcessData() {

    String myDirectoryPath = "/home/myStoredFolder";

    File dir = new File(myDirectoryPath);
    for (File child : dir.listFiles()) {
        String extension = "";

        int j = child.getName().lastIndexOf('.');
        if (j > 0) {
            extension = child.getName().substring(j + 1);
        }


        if (fileIsOktoBeImported) {

            // Import the file into the database

                            import.loadDatabase();              


            // Rename the file after processing
        }

        else

        {
            System.out.println("No processing required on file "
                    + child.getName());
        }
    }

}

}

另外,我有另一个类,它读取 excel 文件并通过 JPA 和实体管理器将数据保存到数据库中。这本身就可以正常工作(我将它链接到 GUI 并可以从那里导入)但我需要在我的 ServletContextListener 中调用 loadDatabase() 方法来导入新的 excel 文件。我试图注入将 ImportDatabase 导入 ServletContextListener 并调用 loadDatabase() 方法,但是当 EntityManager 持久保存到数据库时,我得到一个空指针异常。

爪哇

@Stateful
@LocalBean
public class ImportDatabase implements TableDao{
@Inject
private EntityManager em;   
Row row = null;
FileInputStream inp;
Workbook wb;

public void loadDatabase()
{

 Load data into the Database via JPA

}

更新 Java 以包含 EntityManager 生产者

爪哇

@Stateful
@RequestScoped
public class Resources{

    @PersistenceContext(type = PersistenceContextType.EXTENDED)
    private EntityManager em;

    @Produces
    public EntityManager getEm() {
        return em;
    }
}

在 ServetContextListener 中调用我的 loadDatabase() 方法的最佳方法是什么?

非常感谢任何帮助,

更新

我现在在将 ImportDatabase 类注入 servletContextLIsener 时遇到错误

错误

1:15:06,447 错误 [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/mast]](MSC 服务线程 1-1)配置类 com 的应用程序侦听器时出错。 ericsson.listener.StagedFolderListener:java.lang.IllegalStateException:JBAS011048:无法在 org.jboss.as.ee.component.BasicComponent.constructComponentInstance 构造组件实例(BasicComponent.java:163)[jboss-as-ee-7.1.1 .Final.jar:7.1.1.Final] 在 org.jboss.as.ee.component.BasicComponent.createInstance(BasicComponent.java:85) [jboss-as-ee-7.1.1.Final.jar:7.1.1 .Final] 在 org.jboss.as.web.deployment.component.WebComponentInstantiator$1.(WebComponentInstantiator.java:57) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]

显然我在这里做错了什么......只是不确定是什么,非常感谢任何帮助!

干杯

4

1 回答 1

2

@EJB尝试使用注释将您的 DAO 注入侦听器:

@EJB ImportDatabase importDatabase;

此外,您不需要同时注入TableDAOand ImportDatabase,选择一个或另一个。最后,您当前的生产者是请求范围的,这将阻止它在 servlet 侦听器的上下文中解析(初始化侦听器时请求范围不会处于活动状态)。我建议删除它的范围。

于 2013-03-26T11:22:46.450 回答