默认情况下,Tomcat的WebSocket最大连接数为200。

WebSocket后台代码

package com.chat.demo;/**  Licensed to the Apache Software Foundation (ASF) under one or more*  contributor license agreements.  See the NOTICE file distributed with*  this work for additional information regarding copyright ownership.*  The ASF licenses this file to You under the Apache License, Version 2.0*  (the "License"); you may not use this file except in compliance with*  the License.  You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0**  Unless required by applicable law or agreed to in writing, software*  distributed under the License is distributed on an "AS IS" BASIS,*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.*  See the License for the specific language governing permissions and*  limitations under the License.*/import java.io.IOException;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicInteger;import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;//import org.apache.juli.logging.Log;
//import org.apache.juli.logging.LogFactory;//import util.HTMLFilter;

@ServerEndpoint(value = "/websocket/chat")
public class ChatAnnotation {//private static final Log log = LogFactory.getLog(ChatAnnotation.class);private static final String GUEST_PREFIX = "Guest";private static final AtomicInteger connectionIds = new AtomicInteger(0);private static final Set<ChatAnnotation> connections =new CopyOnWriteArraySet<ChatAnnotation>();private final String nickname;private Session session;public ChatAnnotation() {nickname = GUEST_PREFIX + connectionIds.getAndIncrement();}@OnOpenpublic void start(Session session) {this.session = session;connections.add(this);String message = String.format("* %s %s", nickname, "has joined.");broadcast(message);}@OnClosepublic void end() {connections.remove(this);String message = String.format("* %s %s",nickname, "has disconnected.");broadcast(message);}@OnMessagepublic void incoming(String message) {// Never trust the clientString filteredMessage = String.format("%s: %s",nickname, message.toString());broadcast(filteredMessage);}@OnErrorpublic void onError(Throwable t) throws Throwable {System.out.println("Chat Error: " + t.toString());}private static void broadcast(String msg) {for (ChatAnnotation client : connections) {try {synchronized (client) {client.session.getBasicRemote().sendText(msg);}} catch (IOException e) {//log.debug("Chat Error: Failed to send message to client", e);
                connections.remove(client);try {client.session.close();} catch (IOException e1) {// Ignore
                }String message = String.format("* %s %s",client.nickname, "has been disconnected.");broadcast(message);}}}
}

启动Tomcat,注意ws的地址,然后运行测试WebSocket连接数代码

package com.jiafuwei.java.snake;import java.net.URI;import javax.websocket.ClientEndpoint;
import javax.websocket.ContainerProvider;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;@ClientEndpoint
public class WebSocketTest {private String deviceId;private Session session;public WebSocketTest () {}public WebSocketTest (String deviceId) {this.deviceId = deviceId;}protected boolean start() {WebSocketContainer Container = ContainerProvider.getWebSocketContainer();String uri = "ws://localhost:8028/lhychat/websocket/chat";System.out.println("Connecting to " + uri);try {session = Container.connectToServer(WebSocketTest.class, URI.create(uri));System.out.println("count: " + deviceId);} catch (Exception e) {e.printStackTrace();return false;}return true;}public static void main(String[] args) {for (int i = 1; i< 1000; i++) {WebSocketTest wSocketTest = new WebSocketTest(String.valueOf(i));if (!wSocketTest.start()) {System.out.println("测试结束!");break;}}}
}

运行结果为每次循环到第200个的时候,测试结束了,WebSocket后台代码开始报错。

如何增加并发数呢,只需在server.xml文件中,增加 maxThreads="300" 这样最大的并发数就变成了300。

<Connector maxConnections="300"  maxThreads="300"  URIEncoding="UTF-8" connectionTimeout="20000" port="8028" protocol="HTTP/1.1" redirectPort="8443" useBodyEncodingForURI="true"/>

参考资料:

http://blog.csdn.net/z915412321/article/details/52261229

http://bbs.csdn.net/topics/390678513

转载于:https://www.cnblogs.com/jiafuwei/p/6383062.html

Tomcat下WebSocket最大连接数测试相关推荐

  1. 环境部署(java安装和配置,Tomcat安装和配置)(tomcat下部署war包)

    1,上传环境部署安装包到服务器上 2,解压安装包,并部署java #  tar -xf jdk-8u201-linux-x64.tar.g # mkdir /usr/java # cp  jdk1.8 ...

  2. linux id高 负载高,linux下的rsync连接数突然增高,负载增高导致服务登录失败

    问题:测试centos5.3系统下的rsync连接数.linux下的rsync连接数突然增高,负载增高导致服务登录失败. 软硬件环境如下: 四台2个四核的CPU,8G内存的刀片服务器,只提供rsync ...

  3. 在Tomcat下http协议转https协议

    Tomcat下http协议转https协议,在腾讯云下载的免费SSL证书 最近在搞微信小程序的支付问题,但是调用支付接口的规则是传输规则是必须为https传输,因为我本身是Javaweb项目,发布在T ...

  4. 在Tomcat下JSP、Servlet和JavaBean环境的配置

    经常看到jsp的初学者问tomcat下如何配置jsp.servlet和bean的问题,于是总结了一下如何tomcat下配置jsp.servlet和ben,希望对那些初学者有所帮助. 第一步:下载j2s ...

  5. 利用cors,实现js跨域访问Tomcat下资源

    第一步:页面js代码: function createCORSRequest(method, url){var xhr = new XMLHttpRequest();if ("withCre ...

  6. Tomcat下java普通类IO文件路径问题

    由于在windows和linux下文件路径的表示方式存在差异 而我们的项目大多是在windows下的eclipse中完成测试 然后部署到linux的tomcat服务器中 这个时候我们既不能把地址写死( ...

  7. 帆软10.0服务器Tomcat 下通过 IP 直接访问数据决策系统出错

    帆软服务器Tomcat 下通过 IP 直接访问数据决策系统出错恢复 方法一: Tomcat 下通过 IP 直接访问数据决策系统- FineReport帮助文档 - 全面的报表使用教程和学习资料 方法二 ...

  8. web部署到服务器显示404,WAR包部署到服务器的tomcat下404报错

    问题描述:后端项目开发工具是IntelliJ IDEA,运行web项目,本地测试项目能够正常运行.将项目打包成war包之后放到本机的tomcat的webapps下报404,原因应该是版本太低,7版本和 ...

  9. Tomcat下JSP、Servlet和JavaBean环境的配置

    一.开发环境配置 第一步:下载j2sdk和tomcat:到sun官方站(http://java.sun.com/)下载j2sdk,注意下载版本,同时最好下载J2SE  Documentation,然后 ...

最新文章

  1. Linux驱动技术(一) _内存申请
  2. WCF方法拦截及OperationInvoker传递参数到WCF方法的实现
  3. ERROR 1045 (28000): Access denied for user 'ODBC'@'localhost' 解决Mysql错误
  4. vue2.0中的:is和is的区别
  5. 判断奇偶性 大数(高精度)
  6. Layui--颜色选择器layui.colorpicker
  7. Ubuntu 20.10 wine、微信、QQ安装教程
  8. 快速开发一个混合APP(Hybrid APP)
  9. 编译龙芯PMON流程
  10. 9种免费在线PDF编辑网站
  11. 程序员这口饭,职业规划解决方案---程序员职业规划(二)
  12. OpenStack DVR 原理深入分析
  13. css插件载进去ps里面,CSS3Ps(ps图层插件)官方版
  14. 专治不明觉厉:深度解密IBM黑科技量子计算机
  15. 令人心酸的100个微瞬间 不信你不会被触动 别落泪哦
  16. 红米K30S至尊纪念版和红米K30至尊纪念版哪个好
  17. MATLAB 画点图即连线
  18. python 自动记录时间_python记录程序运行时间的几种方法
  19. 字符型指针,数组,字符串赋值
  20. 哪种性格的人,更适合转管理?

热门文章

  1. springboot map数据类型注入_Spring Boot(五):春眠不觉晓,Mybatis知多少
  2. c语言饭卡服务程序设计思路,C语言设计—饭卡管理程序.doc
  3. grep 去掉 grep_使用grep的regex的10个实用示例
  4. python日期时间_Python日期时间
  5. primefaces教程_Primefaces仪表板组件示例教程
  6. 字符串太长 pep8_Python f字符串– PEP 498 –文字字符串插值
  7. C++教程:C++开发语言可以做些什么?
  8. Join a New Company
  9. C++实现整数值转中文大写
  10. ngx_lua 模块提供的指令和API等