【发布时间】:2010-11-27 02:05:58
【问题描述】:
编辑:
我现在确定问题与
while (true) 循环保存所有其他命令,因为我已将其注释掉,并且应用程序在没有附加异常的情况下部署。我不确定它有多重要,但我的 ServletContextListener 实现如下所示:
public class BidPushService implements ServletContextListener{
public void contextInitialized(ServletContextEvent sce) {
//Some init code not relevant, omitted for clarity
BidPushThread t= new BidPushThread();
t.setServletContext(sce.getServletContext());
t.run();
}
所以现在线程在应用部署时运行,但是因为 while 循环被注释它没有真正的意义。
当我的应用程序加载时,我需要在后台运行一个线程,并不断(没有超时)检查某个队列中的对象。当然,一旦有对象,它就会“处理它们”,然后继续检查队列。
目前,我正在实现ServletContextListener 接口,并在应用加载时被调用。在其中,我做了一些维护工作并启动了一个从java.lang.Thread 继承的线程。
这是我的问题开始的地方(或者我认为)。在我的run() 方法中,我有一个
while (true) {
//some code which doesn't put the thread to sleep ever
}
当我尝试将我的应用程序部署到服务器时,我得到一个java.util.concurrent.TimeOutException。
我做错了什么?
我不能有一个一直在运行的线程吗?当应用程序被删除时,该线程会被我的ServletContextListener 中的相应事件停止。
我确实需要一些东西,可以毫不拖延地不断检查队列。
非常感谢您的帮助!
编辑:这是堆栈跟踪
GlassFish: deploy is failing=
java.util.concurrent.TimeoutException
at java.util.concurrent.FutureTask$Sync.innerGet(Unknown Source)
at java.util.concurrent.FutureTask.get(Unknown Source)
at com.sun.enterprise.jst.server.sunappsrv.SunAppServerBehaviour.publishDeployedDirectory(SunAppServerBehaviour.java:710)
at com.sun.enterprise.jst.server.sunappsrv.SunAppServerBehaviour.publishModuleForGlassFishV3(SunAppServerBehaviour.java:569)
at com.sun.enterprise.jst.server.sunappsrv.SunAppServerBehaviour.publishModule(SunAppServerBehaviour.java:266)
at org.eclipse.wst.server.core.model.ServerBehaviourDelegate.publishModule(ServerBehaviourDelegate.java:948)
at org.eclipse.wst.server.core.model.ServerBehaviourDelegate.publishModules(ServerBehaviourDelegate.java:1038)
at org.eclipse.wst.server.core.model.ServerBehaviourDelegate.publish(ServerBehaviourDelegate.java:872)
at org.eclipse.wst.server.core.model.ServerBehaviourDelegate.publish(ServerBehaviourDelegate.java:708)
at org.eclipse.wst.server.core.internal.Server.publishImpl(Server.java:2690)
at org.eclipse.wst.server.core.internal.Server$PublishJob.run(Server.java:272)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)
我的代码:
public class BidPushThread extends Thread {
private ServletContext sc=null;
@Override
public void run() {
if (sc!=null){
final Map<String, List<AsyncContext>> aucWatchers = (Map<String, List<AsyncContext>>) sc.getAttribute("aucWatchers");
BlockingQueue<Bid> aucBids = (BlockingQueue<Bid>) sc.getAttribute("aucBids");
Executor bidExecutor = Executors.newCachedThreadPool();
final Executor watcherExecutor = Executors.newCachedThreadPool();
while(true)
{
try // There are unpublished new bid events.
{
final Bid bid = aucBids.take();
bidExecutor.execute(new Runnable(){
public void run() {
List<AsyncContext> watchers = aucWatchers.get(bid.getAuctionId());
for(final AsyncContext aCtx : watchers)
{
watcherExecutor.execute(new Runnable(){
public void run() {
// publish a new bid event to a watcher
try {
aCtx.getResponse().getWriter().print("A new bid on the item was placed. The current price "+bid.getBid()+" , next bid price is "+(bid.getBid()+1));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
});
}
}
});
} catch(InterruptedException e){}
}
}
}
public void setServletContext(ServletContext sc){
this.sc=sc;
}
}
很抱歉格式混乱,但对于我的“缩进 4 个空格”的生命来说,这对我不起作用 编辑:阅读“BlockingQueue”并实现它,但我仍然得到完全相同的异常和堆栈跟踪。更改了上面的代码以反映'BlockingQueue'的使用
【问题讨论】:
-
您需要向我们提供异常的堆栈跟踪
-
请出示您的代码。 Java API 中很少有类会抛出您收到的异常,并且它们的使用与您提供的描述不符。
-
您绝对应该使用阻塞队列。 java.sun.com/javase/6/docs/api/java/util/concurrent/… 的 JavaDocs 有示例代码。
标签: java multithreading deployment servlets timeout