我编写了一个从SMTP服务器检索邮件的程序。我需要先启动服务器,然后用户可以使用REST API获取信息。
如果Tomcat部署.war,我怎么能运行一个方法来启动SMTP服务器(端口25)?
我试过这样的事情:
@WebListener
public class InitializeListner implements ServletContextListener {
@Override
public final void contextInitialized(final ServletContextEvent sce) {
SMTPServer smtp_server = SMTPServer.getInstance();
smtp_server.start();
}
但它不起作用并抛出:
GRAVE: Erreur lors de la configuration de la classe d'écoute de l'application (application listener)
com.smtp.server.Initializer$InitializeListner
java.lang.InstantiationException: com.smtp.server.Initializer$InitializeListner
at java.lang.Class.newInstance(Unknown Source)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:119)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4733)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5251)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1092)
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1834)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NoSuchMethodException: com.smtp.server.Initializer$InitializeListner.<init>()
at java.lang.Class.getConstructor0(Unknown Source)
... 15 more
谢谢。
编辑:这是我的实际代码
public class SMTPServer extends Wiser
{
private SMTPServer()
{}
private static SMTPServer SMTPServerInstance = null;
public static synchronized SMTPServer getInstance()
{
if (SMTPServerInstance == null)
{
SMTPServerInstance = new SMTPServer();
SMTPServerInstance.setPort(25);
SMTPServerInstance.setHostname("localhost");
}
return SMTPServerInstance;
}
}