Reputation: 2160
Very new to WebSphere as well as packaging Java.
I need to take a Java project that we've been running from the command line as an executable Jar and make it run from WebSphere (since the admin user has been getting auto-logged out at midnight).
I've looked at creating a WAR file, but the ant examples I've looked at invoked a lot of WEB-INF dependencies ... but this project doesn't have that. It has a main entry point in the code.
EARs seem to require EJBs, which I don't think this project uses. Seeing as EJBs have been on their way out for a while I'm not as up to speed on them.
My questions are: What is the simplest way to put my executable JAR into WebSphere?
Bonus points: Do EAR files require EJBs in the project? If so, how do I know if this project invokes them?
Upvotes: 2
Views: 1050
Reputation: 1028
The simple answer is to create a war
application with a @WebListener:
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener
public class ServletInitializer implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println(toString() + " contextInitialized started");
String[] args = new String[] {};
MainClass.main(args);
System.out.println(toString() + " contextInitialized finished");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}
Replace MainClass.main(args);
with your application call (and add any JAR dependencies into WEB-INF/lib
).
The thing I don't like about the above is that it's not great to perform intense work during startup. This may make problem determination harder and certain capabilities (e.g. runtime diagnostic trace modification) are unavailable during startup.
You could create a WAS-specific solution by using AdminClient to subscribe to the Server MBean's Stateful notifications.
Otherwise, a standardized way would be to have an EJB @Startup bean which uses the TimerService to start work after some delay which is empirically determined to be an average application server startup time. For example:
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerConfig;
import javax.ejb.TimerService;
@Singleton
@Startup
public class DeferredInitializer {
public static final int DELAY = Integer.getInteger("DeferredInitializer.DELAY", 60000);
@Resource
TimerService timerService;
@PostConstruct
private void init() throws Throwable {
System.out.println(toString() + " init entry");
TimerConfig timerConfig = new TimerConfig();
timerConfig.setPersistent(false);
timerService.createSingleActionTimer(DELAY, timerConfig);
Calendar c = new GregorianCalendar();
c.add(Calendar.MILLISECOND, DELAY);
System.out.println(toString() + " timer estimated to pop in (" + DELAY + "ms) ~@ " + c.getTime());
System.out.println(toString() + " init exit");
}
@Timeout
public void timeout(Timer timer) {
System.out.println(toString() + " timeout entry");
String[] args = new String[] {};
MainClass.main(args);
System.out.println(toString() + " timeout exit");
}
}
Specify the delay with the generic JVM argument -DDeferredInitializer.DELAY=X
(X in milliseconds).
Upvotes: 1