在我的web应用程序中,websocket通信有问题。有时,当我尝试向多个客户机发送消息时(通常我们有30-40个客户机),应用程序就会卡住。查看日志文件,似乎所有内容都挂在“broadcastmessage”方法上(在该方法中,我使用了同步锁)。
下面是这些类的实现。
websocket:
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint("/websocket")
public class WebSocketServer
{
private static Set<Session> clients = Collections.synchronizedSet(new HashSet<Session>());
@OnMessage
public void onMessage(String message, Session session) throws IOException, InterruptedException
{
if (message.equals("ping"))
{
session.getBasicRemote().sendText("pong");
}
}
@OnOpen
public void onOpen(Session session)
{
clients.add(session);
}
@OnClose
public void onClose(Session session)
{
clients.remove(session);
}
public static void broadcastMessage(String message) throws IOException, InterruptedException
{
broadcastMessage(message, null);
}
private static void broadcastMessage(String message, Session session) throws IOException, InterruptedException
{
synchronized (clients)
{
for (Session client : clients)
{
if (session == null || !client.equals(session))
{
client.getBasicRemote().sendText(message);
}
}
}
}
}
然后我在服务器启动时安排了一个servlet,它安排了一个线程(每秒钟):
import java.io.IOException;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet extends HttpServlet
{
private static final long serialVersionUID = 5064961381596268407L;
@Override
public void init() throws ServletException
{
Long period = Long.parseLong(1000);
new MyThread().schedule(new Date(), period);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
}
}
这是线程:
import java.io.File;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.log4j.Logger;
public class MyThread
{
private static Timer timer;
private String PATH = "C:\\Users\\administrator\\Pictures";
private Logger log = Logger.getLogger("pic");
public void schedule(Date date, Long period)
{
if (timer != null)
{
timer.cancel();
}
timer = new Timer();
if (date == null)
{
date = new Date();
}
if (period != null)
{
timer.schedule(new ThreadLogger(), date, 1000 * period.longValue());
}
else
{
timer.schedule(new ThreadLogger(), date);
}
}
public class ThreadLogger extends TimerTask
{
public void run()
{
log.debug("MyThread.run ===========================================================");
log.debug("Directory => " + PATH);
File dir = new File(PATH);
FileFilterDateIntervalUtils filter = new FileFilterDateIntervalUtils(new Date());
File[] files = dir.listFiles(filter);
if (files != null && files.length > 0)
{
Arrays.sort(files, new Comparator<File>()
{
public int compare(File f1, File f2)
{
return Long.valueOf(f2.lastModified()).compareTo(f1.lastModified());
}
});
try
{
log.debug("Found file => " + files[0].getName());
WebSocketServer.broadcastMessage(files[0].getName());
}
catch (Exception e)
{
log.error("Error: " + e.getMessage(), e);
}
}
else
{
log.debug("Alert: 0 files");
}
log.debug("================================================================================");
}
}
}
websocket的方法broadcastmessage仅由调度线程调用!每个客户端每分钟发送一条ping消息。
你知道应用程序为什么会卡住吗?我该怎么解决?
谢谢!
暂无答案!
目前还没有任何答案,快来回答吧!