I have a ServletContextListener that is running on start up of my JBOSS 7.1 server. This essentially listens on a folder and waits for new files and waits for new excel files at present and renames them.
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());
}
}
}
}
Separately i have another class that reads in excel files and persists the data to the database via JPA and an Entity Manager. This works fine on its own right (i have it linked to a GUI and can import from there) but i need to have the loadDatabase() method called within my ServletContextListener to import the new excel files that come in. I have tried to inject the ImportDatabase into the ServletContextListener and call the loadDatabase() method but i get a null pointer exception when the EntityManager persists to the database.
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
}
Updating Java to include EntityManager producer
Java
@Stateful
@RequestScoped
public class Resources{
@PersistenceContext(type = PersistenceContextType.EXTENDED)
private EntityManager em;
@Produces
public EntityManager getEm() {
return em;
}
}
What is the best way to have my loadDatabase() method called in the ServetContextListener?
Any help much appreciated,
update
Im now getting an error when injecting the ImportDatabase class into the servletContextLIstener
Error
1:15:06,447 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/mast]] (MSC service thread 1-1) Error configuring application listener of class com.ericsson.listener.StagedFolderListener: java.lang.IllegalStateException: JBAS011048: Failed to construct component instance at 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]
Obviously im doing something wrong here ...just not sure what, any help much appreciated!
Cheers