JBOSS7.1サーバーの起動時に実行されているServletContextListenerがあります。これは基本的にフォルダをリッスンし、新しいファイルを待機し、現在新しいExcelファイルを待機して名前を変更します。
Java
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にリンクされており、そこからインポートできます)が、新しいExcelファイルをインポートするには、ServletContextListener内でloadDatabase()メソッドを呼び出す必要があります。 ImportDatabaseをServletContextListenerに入れ、loadDatabase()メソッドを呼び出しますが、EntityManagerがデータベースに永続化されると、nullポインター例外が発生します。
Java
@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
}
EntityManagerプロデューサーを含むようにJavaを更新する
Java
@Stateful
@RequestScoped
public class Resources{
@PersistenceContext(type = PersistenceContextType.EXTENDED)
private EntityManager em;
@Produces
public EntityManager getEm() {
return em;
}
}
私のloadDatabase()メソッドをServetContextListenerで呼び出すための最良の方法は何ですか?
どんな助けでも大歓迎です、
アップデート
ImportDatabaseクラスをservletContextLIstenerに挿入するとエラーが発生するようになりました
エラー
1:15:06,447エラー[org.apache.catalina.core.ContainerBase。[jboss.web]。[default-host]。[/ must]](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] at org.jboss.as.ee.component.BasicComponent.createInstance(BasicComponent.java:85)[jboss-as-ee-7.1.1.Final.jar:7.1.1 .Final] at org.jboss.as.web.deployment.component.WebComponentInstantiator $ 1。(WebComponentInstantiator.java:57)[jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
明らかに、ここで何か間違ったことをしているのです...何がわからないのか、助けていただければ幸いです。
乾杯