部署Web项目时如何在Java中识别?

时间:2015-10-29 13:19:11

标签: java tomcat

我有一个简单的网页项目,只需点击一下按钮就可以改变另一行文字。我想要的是一个core Java方法,它将识别何时在Tomcat服务器上部署.war文件。

我想输出部署发生的确切时间。我准备好了代码,我希望它在core Java

是否有任何此类方法可以让我在部署发生时执行某些活动?

2 个答案:

答案 0 :(得分:2)

您可以使用ServletContextListener

// Database name definitions public static final String DATABASE_NAME = "MMPDB.db"; // Table name definitions public static final String LOG_IN_TABLE = "LogIn"; public static final String FI_GOODS_DETAIL_TABLE = "fiGoodDetail"; public static final String FI_GOODS_SUMMARY_TABLE = "fiGoodSummary"; public static final String ITEM_DETAIL_TABLE = "itemDetail"; public static final String SETTINGS_TABLE = "Setting"; // LogIn table fields declaration public static final String LOGIN_USERNAME = "userName"; public static final String LOGIN_PASSWORD = "password"; // fiGoodDetail table fields declaration public static final String FIGOODSDETAIL_FINISHGOODS = "fiGoods"; public static final String FIGOODSDETAIL_ITEMDESC = "itemDesc"; public static final String FIGOODSDETAIL_FINISHQTY = "fiQty"; public static final String FIGOODSDETAIL_FINISHUNITWEIGHT = "fiUnitWeight"; public static final String FIGOODSDETAIL_FINISHTOTALWEIGHT = "fiTotalWeight"; public static final String FIGOODSDETAIL_LOTBINNUM = "lotBinNum"; // fiGoodSummary table fields declaration public static final String FIGOODSSUMMARY_FINISHGOODS = "fiGoods"; public static final String FIGOODSSUMMARY_FINISHQTY = "fiQty"; public static final String FIGOODSSUMMARY_FINISHUNITWEIGHT = "fiUnitWeight"; public static final String FIGOODSSUMMARY_FINISHTOTALWEIGHT = "fiTotalWeight"; public static final String FIGOODSSUMMARY_ITEMDESC = "itemDesc"; public static final String FIGOODSSUMMARY_QTYUM = "qtyUm"; public static final String FIGOODSSUMMARY_QTY = "qty"; public static final String FIGOODSSUMMARY_YIELD = "yield"; // itemDesc table fields declaration public static final String ITEMDETAIL_FINISHGOODS = "fiGoods"; public static final String ITEMDETAIL_ITEMDESC = "itemDesc"; public static final String ITEMDETAIL_LOCTID = "loctId"; public static final String ITEMDETAIL_LOTBINNUM = "lotBinNum"; public static final String ITEMDETAIL_QTYUM = "qtyUm"; public static final String ITEMDETAIL_QTYUMUSED = "qtyUmUsed"; public static final String ITEMDETAIL_YIELD = "yield"; public static final String ITEMDETAIL_ACTUALQTY = "actQty"; public static final String ITEMDETAIL_ACTUALSCARP = "actScrp"; public static final String ITEMDETAIL_UNITCOST = "unitCost"; public static final String ITEMDETAIL_TOTALCOST = "totalCost"; // Setting table fields declaration public static final String SETTINGS_SERVERPATH = "serverPath"; // public static final String SETTINGS_COMPANY="company"; // Creation of Query for Each Table. // tlogin table creation query public static final String LOG_IN_TABLE_CREATE_QUERY = "CREATE TABLE " + LOG_IN_TABLE + "(" + LOGIN_USERNAME + " TEXT," + LOGIN_PASSWORD + " TEXT);"; // tfigodet table creation query public static final String FI_GOODS_DETAIL_TABLE_CREATE_QUERY = "CREATE TABLE " + FI_GOODS_DETAIL_TABLE + "(" + FIGOODSDETAIL_FINISHGOODS + " TEXT," + FIGOODSDETAIL_ITEMDESC + " TEXT," + FIGOODSDETAIL_FINISHQTY + " TEXT," + FIGOODSDETAIL_FINISHUNITWEIGHT + " TEXT," + FIGOODSDETAIL_FINISHTOTALWEIGHT + " TEXT," + FIGOODSDETAIL_LOTBINNUM + " TEXT);"; // tfigosum table creation query public static final String FI_GOODS_SUMMARY_TABLE_CREATE_QUERY = "CREATE TABLE " + FI_GOODS_SUMMARY_TABLE + "(" + FIGOODSSUMMARY_FINISHGOODS + " TEXT," + FIGOODSSUMMARY_FINISHQTY + " TEXT," + FIGOODSSUMMARY_FINISHUNITWEIGHT + " TEXT," + FIGOODSSUMMARY_FINISHTOTALWEIGHT + " TEXT," + FIGOODSSUMMARY_ITEMDESC + " TEXT," + FIGOODSSUMMARY_QTYUM + " TEXT," + FIGOODSSUMMARY_QTY + " TEXT," + FIGOODSSUMMARY_YIELD + " TEXT);"; // titemdet table creation query public static final String ITEM_DETAIL_TABLE_CREATE_QUERY = "CREATE TABLE " + ITEM_DETAIL_TABLE + "(" + ITEMDETAIL_FINISHGOODS + " TEXT," + ITEMDETAIL_ITEMDESC + " TEXT," + ITEMDETAIL_LOCTID + " TEXT," + ITEMDETAIL_LOTBINNUM + " TEXT," + ITEMDETAIL_QTYUM + " TEXT," + ITEMDETAIL_YIELD + " TEXT," + ITEMDETAIL_ACTUALQTY + " TEXT," + ITEMDETAIL_QTYUMUSED + " TEXT," + ITEMDETAIL_ACTUALSCARP + " TEXT," + ITEMDETAIL_UNITCOST + " TEXT," + ITEMDETAIL_TOTALCOST + " TEXT);"; // tsettings table creation query public static final String SETTINGS_TABLE_CREATE_QUERY = "CREATE TABLE " + SETTINGS_TABLE + "(" + SETTINGS_SERVERPATH + " TEXT);"; // Constructor to create Database public MmpDBHelper(Context context) { DATABASE_FILE_PATH = new File(Supporter.getAppCommonPath(), DATABASE_NAME); if (!DATABASE_FILE_PATH.exists()) { try { db = SQLiteDatabase.openOrCreateDatabase(DATABASE_FILE_PATH, null); // openDatabase(); db.beginTransaction(); db.execSQL(LOG_IN_TABLE_CREATE_QUERY); db.execSQL(FI_GOODS_DETAIL_TABLE_CREATE_QUERY); db.execSQL(FI_GOODS_SUMMARY_TABLE_CREATE_QUERY); db.execSQL(ITEM_DETAIL_TABLE_CREATE_QUERY); db.execSQL(SETTINGS_TABLE_CREATE_QUERY); db.setTransactionSuccessful(); Log.i("Success", "All tables created successfully"); } catch (Exception e) { db.endTransaction(); errCode = "Error 501"; msg = "Table creation failed"; errMsg = errCode + " : " + msg; LogFileCreator.appendLog(errMsg); } finally { // db.endTransaction(); db.close(); } } } 文件中添加<listener>标记,如下所示:

web.xml

并使<listener> <listener-class>classes.MyListener</listener-class> </listener> 类实现Java接口。覆盖ServletContextListener方法并在其中编写代码就可以了。

例如:

contextInitialized()

希望这有帮助!

答案 1 :(得分:1)

查看是否已在webapps目录中解压缩(展开)WAR文件。通过查看爆炸目录是否存在,可以通过两种不同的方式完成此操作。或者通过查看WAR文件的访问时间。请注意,要查看* nix中文件的访问时间,您必须使用-u命令中的ls选项。

但是这有一个缺陷,你无法分辨web服务何时完成初始化。为此,您可以创建一个keepAlive服务,您可以在Web服务中进行轮询。这样,当您的应用程序完成初始化时,您将获得keepAlive的响应。您可以在此服务上设置高负载数,以允许首先加载所有其他Servlet。

curl "http:localhost:8080/MyWebService/keepAlive"

此外,我特别了解Tomcat,您可以查询其JMX监视器。我不知道它是否有一个JMX查询来列出已加载的Servlet,但它可能并且它是可扩展的,因此您可以添加自己的查询以找出它。

编辑:您还可以设置Servlet在初始化时进行记录。然后在循环中只有grep "your services' log msg",直到它以退出代码0退出。