0
votes

we have been working with IBM Worklight for some time now and we have run in the following issue.

When our server starts we want it to run some initial code to fill and update our database with data. This is a process that runs once everyday. But in order to do this we need to be able to start a cron job or something simular.

We know how to use cron jobs and stuff like that. But what we have not managed is to have this cronjob to start once the work light server is started.

so to summarize my question:

How can we run code on the worklight server automaticly once the work light server has started?

private void code(){
     //Run this code once Worklight server is started
}

Some aditional information on 07-03-2014:

So i have tried the ServletContextListener but no success. Tried to print something in the logger so i could see if my Importer class is running. here is what i used

Java file:

public class Importer implements ServletContextListener{

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        // TODO Auto-generated method stub
        Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
        logger.warning("running this shizzle :D");

    }
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        // TODO Auto-generated method stub

    }
}

web.xml file added and placed back in the .war package:

<listener>  
    <listener-class>com.ibm.nl.wwdw.server.connections.Importer</listener-class>    
</listener>

console log:

[2014-03-07 12:59:22] Worklight Server started successfully on localhost:8080 [2014-03-07 12:59:22]
Activating Worklight project: mobile-app... [2014-03-07 12:59:25] FWLSE2001I: Detected more than one IP address. Using 192.168.75.3 as the public IP address of the Worklight Server. You can set a different IP address in worklight.properties. [2014-03-07 12:59:25] FWLSE3005I: Application raw reports are disabled. [2014-03-07 12:59:25] FWLST0010I: ====== Started server for project mobile-app-project-customization; Worklight version=5.0.5.20121130-0158 [2014-03-07 12:59:25]
Activation done.

1
What is the underlying server? WAS liberty, full profile, or tomcat? Do you need the startup code to be in JavaScript, or would Java work for you?David Dhuyveter
It runs on websphere as the underlaying server. i would like the code to run in java.Raggy

1 Answers

0
votes

I would create a java class that implements ServletContextListener.. It has a method called contextInitialized that is fired when the servlet container first fires up. http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContextListener.html

You would have to add the context listener to the web.xml after its generated:

<listener>
    <listener-class>
        com.yourcomp.project.ContextListenerClass
    </listener-class>
</listener>