- 浏览: 216327 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
a66756675:
...
tomcat:tomcat的OutOfMemoryError解决 -
ooo456mmm:
说的对,如果用Mina框架来处理,要简单很多了
NIO socket服务器注意的几点. -
long_ltoy:
这样的话,看上去是代替了session,但这不和session ...
在JSP中使用JAVABEAN代替Session -
linzx0212:
关注下,不错……
tomcat:tomcat的OutOfMemoryError解决 -
liudeh_009:
总结得很好
NIO socket服务器注意的几点.
-- 发布时间:2005-10-20 22:06:01
-- 计数器系统
有一计数器系统要求如下:
1性能要求:每天访问量要求符合100000/天 且有30-40 个站点统计 同时进行
2刷新量,纯IP统计(0-24 每个时间段内)各自形成每天报表
本人的解决方案:
第一次读数据库,之后的数据都存在APLICATION中进行加一操作定时更新数据库 三四十个站点同时进行。
问题:发现Aplication 有不稳定的情况(同一Aplication 有时候一会儿变成这个数据 一会儿变成那个数据,之间的差值会虽时间长而变大,但是还是最大的那个正确,就是检查不出来怀疑数据访问量大的时候Tomcat 没有处理好Aplication这个东西)如果把这个程序单独每个统计站布署一个就不会出现这种情况。注:我的程序不存在性能瓶经基本全在内存中操作速度很快服务器硬件也没有问题。
谁能用JAVA来实现统一计数实现我如上的条件,或者能解决一下我的问题所在,本人将以高分数表示感谢,希望写过大型计数器的朋友进入
本人写了一个Servlet 不怕大家见笑抛砖引玉:
以下是本人前100行代码(程序有不完善的地方见笑。。。)
import java.sql.*;
import javax.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.PageContext;
import java.io.*;
import java.util.Date;
import java.text.DateFormat;
import com.inberkong.util.DataBaseConnection;
public class CounterServlet extends HttpServlet
{
private String counterSite;
//private int count=0;
private Connection con=null;
private String pString ="";
private final int WRITE_TIME=6;//定时写数据库的间隔时长以秒为单位
private ServletContext application=null;
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html;charset=gb2312");
String theIP=request.getRemoteAddr();//得到访问者IP;
Date time1=new java.util.Date();//获得系统当前时间
String Referer=request.getHeader("Referer");//得到来访者地址
if(Referer==null)
{
System.out.print("No Referer value");
}
// String Referer="http://61.163.234.223/zhaotie/test1.htm";//\' target="_blank" >http://61.163.234.223/zhaotie/test1.htm";//摸拟得到来访者地址(检验数据开始要把此项打开)
String [] siteAddress=Referer.split("/");
counterSite=siteAddress[0]+"//"+siteAddress[2];//得到来访者的网址域名
PrintWriter out =response.getWriter();
String sql="";
Date tm=new java.util.Date();//获得系统当前时间
String sqltm=new Timestamp(tm.getTime()).toString();//将当前时间转换成sql server可以识别的对象
String st2=sqltm;
st2=st2.substring(0,19);//2005-08-14 18:41:33
String d1=st2.substring(0,10);
application = getServletConfig().getServletContext();
try
{
if(application.getAttribute(counterSite+"_checkAccount")==null)//初始计数器的状态.
{
//application.setAttribute(counterSite,new Integer(count));//计数器
Statement stmt=null;
try{
//如果数据库缓冲池无连接则重新启动TOMCATE服务
stmt=con.createStatement();
}catch(SQLException ee)
{
destroy();
Runtime rn=Runtime.getRuntime();
Process p=null;
try{
p=rn.exec("tomcat5.exe stop");
for(int i=0;i<1999999999;i++)
{
;
}
p=rn.exec("taskkill /F /IM tomcat5w.exe /T");
p=rn.exec("taskkill /F /IM tomcat5.exe /T");
for(int j=0;j<1999999999;j++)
{
;
}
p=rn.exec("tomcat5.exe start");
}catch(Exception e){
System.out.println("Error exec notepad");
}
以下为101-200行代码
System.out.print(ee+""+new java.util.Date());
}
ResultSet rs=null;
application.setAttribute(counterSite+"Date1",new java.util.Date());//站点定时时间
sql="select id,R_TODAY_SUM,R_TOM_SUM,R_THIS_MONTH_SUM,R_TOTAL_SUM,R_TOTAL_DAY,R_COUNT_DATE from inber_count_account where ACCOUNT_SIT_ADD like \'%"+counterSite+"%\'";
//out.print(sql);
rs=stmt.executeQuery(sql);
if(rs.next())
{
application.setAttribute(counterSite+"_checkAccount",new String("ok"));
application.setAttribute(counterSite+"_id",new Integer(rs.getInt(1)));
application.setAttribute(counterSite+"_R_TODAY_SUM",new Integer(rs.getInt(2)));
application.setAttribute(counterSite+"_R_TOM_SUM",new Integer(rs.getInt(3)));
application.setAttribute(counterSite+"_R_THIS_MONTH_SUM",new Integer(rs.getInt(4)));
application.setAttribute(counterSite+"_R_TOTAL_SUM",new Integer(rs.getInt(5)));
application.setAttribute(counterSite+"_R_TOTAL_DAY",new Integer(rs.getInt(6)));
//****
application.setAttribute(counterSite+"_R_COUNT_DATE",new String(rs.getString("R_COUNT_DATE")));
}
rs.close();
stmt.close();
}
else
{
if((application.getAttribute(counterSite+"_checkAccount").toString()).equals("ok"))//校验此计数器账号引用是否存在
{
Date accessTime=null;
//刷新率 start----------------------------------
application.setAttribute(counterSite+"_R_TODAY_SUM",new Integer(Integer.parseInt((application.getAttribute(counterSite+"_R_TODAY_SUM").toString()))+1));
application.setAttribute(counterSite+"_R_THIS_MONTH_SUM",new Integer(Integer.parseInt((application.getAttribute(counterSite+"_R_THIS_MONTH_SUM").toString()))+1));
application.setAttribute(counterSite+"_R_TOTAL_SUM",new Integer(Integer.parseInt((application.getAttribute(counterSite+"_R_TOTAL_SUM").toString()))+1));
String d2=application.getAttribute(counterSite+"_R_COUNT_DATE").toString();
//当日期改变时,清除当天的计数
String tYear,tMonth,tDay;
tYear=d2.substring(0,4);
tMonth=d2.substring(5,7);
tDay=d2.substring(8,10);
if(!(d1.equals(d2)))
{
int todaysum=Integer.parseInt((application.getAttribute(counterSite+"_R_TODAY_SUM").toString()));
int totalday=Integer.parseInt((application.getAttribute(counterSite+"_R_TOTAL_DAY").toString()));
application.setAttribute(counterSite+"_R_TOM_SUM",new Integer(todaysum));
application.setAttribute(counterSite+"_R_TODAY_SUM",new Integer(0));
application.setAttribute(counterSite+"_R_TOTAL_DAY",new Integer(totalday+1));
application.setAttribute(counterSite+"_R_COUNT_DATE",new String(d1));
try{
CallableStatement cstmt4=null;
pString = "{call p_setDetailList3(\'"+tYear+"\', \'"+tMonth+"\', \'"+tDay+"\',"+todaysum+", \'"+d2+"\',"+Integer.parseInt((application.getAttribute(counterSite+"_id").toString()))+")}";
cstmt4=con.prepareCall(pString);
cstmt4.executeUpdate();
cstmt4.close();
}
catch(Exception ee)
{
System.out.println("call p_setDetailList3 is wrong:"+ee);
}
}
String cMonth=d1.substring(5,7);
//当月改变时清除当月的计数,并计数当月第一个浏览者.
if(!(cMonth.equals(tMonth)))
{
application.setAttribute(counterSite+"_R_THIS_MONTH_SUM",new Integer(0));
application.setAttribute(counterSite+"_R_COUNT_DATE",new String(d1));
}
//刷新率 end----------------------------------
201-3百行以后的代码
//count=Integer.parseInt((application.getAttribute(counterSite).toString()));
////////////////////////////////////////////定时写数据库同时 初始化数据 start/////////
Date time2=new java.util.Date();//获得系统当前时间
Date t1=(Date)application.getAttribute(counterSite+"Date1");
int secondValue=(int)(((double)time2.getTime()-(double)t1.getTime())/1000);
if(secondValue>=WRITE_TIME)//
{//定时写数据库同时 初始化数据
int siteid=Integer.parseInt((application.getAttribute(counterSite+"_id").toString()));
int a1=Integer.parseInt((application.getAttribute(counterSite+"_R_TODAY_SUM").toString()));
int a2=Integer.parseInt((application.getAttribute(counterSite+"_R_TOM_SUM").toString()));
int a3=Integer.parseInt((application.getAttribute(counterSite+"_R_THIS_MONTH_SUM").toString()));
int a4=Integer.parseInt((application.getAttribute(counterSite+"_R_TOTAL_SUM").toString()));
int a5=Integer.parseInt((application.getAttribute(counterSite+"_R_TOTAL_DAY").toString()));
String a6=application.getAttribute(counterSite+"_R_COUNT_DATE").toString();
try{
CallableStatement cstmt5=null;
pString = "{call p_INBER_COUNT_ACCOUNT_EDIT2("+siteid+","+a1+","+a2+","+a3+","+a4+","+a5+",\'"+a6+"\')}";
cstmt5=con.prepareCall(pString);
cstmt5.executeUpdate();
cstmt5.close();
}
catch(Exception ee)
{
System.out.println("call p_INBER_COUNT_ACCOUNT_EDIT2 is wrong:"+ee);
}
//count=0;
//application.setAttribute(counterSite,new Integer(count));//得到初始值,
application.setAttribute(counterSite+"Date1",new java.util.Date());//初始时间为系统当前时间
//application.setAttribute(counterSite,new Integer(count));//计数器
////////////////////////////////////////////定时写数据库同时 初始化数据 end//////////////////
}
//////////////检验数据2开始///////////////////////////////////////////////////////////
/*out.print("secondValue="+secondValue+"<BR>");
out.print("counter="+count+"<BR>"+"counterSite="+counterSite+"<BR>");
*/
//////////////检验数据2结束///////////////////////////////////////////////////////////
}
else
{//校验此计数器账号引用是否存在
}
}
//System.out.println(count+":"+counterSite+" "+theIP);
// count++;
//application.setAttribute(counterSite,new Integer(count));
} catch(SQLException e)
{
System.out.println("cc:"+e);
}
out.close();
}
/**
*处理Post请求
*/
/**
*获得初始化参数
*/
public void init() throws ServletException
{
try{
int ss=0;
con=DataBaseConnection.getConnection();
}
catch (Exception e)
{
System.out.println("Error(con error):"+e);
}
}
public void destroy()
{
int siteid=Integer.parseInt((application.getAttribute(counterSite+"_id").toString()));
int a1=Integer.parseInt((application.getAttribute(counterSite+"_R_TODAY_SUM").toString()));
int a2=Integer.parseInt((application.getAttribute(counterSite+"_R_TOM_SUM").toString()));
int a3=Integer.parseInt((application.getAttribute(counterSite+"_R_THIS_MONTH_SUM").toString()));
int a4=Integer.parseInt((application.getAttribute(counterSite+"_R_TOTAL_SUM").toString()));
int a5=Integer.parseInt((application.getAttribute(counterSite+"_R_TOTAL_DAY").toString()));
String a6=application.getAttribute(counterSite+"_R_COUNT_DATE").toString();
try{
CallableStatement cstmt6=null;
pString = "{call p_INBER_COUNT_ACCOUNT_EDIT2("+siteid+","+a1+","+a2+","+a3+","+a4+","+a5+",\'"+a6+"\')}";
cstmt6=con.prepareCall(pString);
cstmt6.executeUpdate();
cstmt6.close();
System.out.println("when the programe stoped execute this code automatic!");
con.close();
}
catch (SQLException e)
{
System.out.println("desdory error:"+e);
}
}
}
=====================
天哪... 没看懂
大概你是遇上并发操作的问题了. 所有的应用服务器都有并发的问题, 主要还是看的应用怎么去处理了. 要注意一下几点
1. 数据库操作得用事务, 而且避免脏读. 如果想避免别人并发操作你的数据, 可以使用锁
2. 对象上注意是否线程安全, 避免多个线程同时写变量
3. 更新数据使用相对值, 不要用绝对值. 如: update table set a = a + @value where ...
100000/天访问不算大, 如果没有并发峰值限制. 不需要太特殊得处理方法
http://community.csdn.net/Expert/topic/4332/4332118.xml?temp=.1796991
发表评论
-
DHTML应用一撇改变网页中元素(Inber原作)
2004-08-19 12:00 813利用DHTML技术我们可以改变网页中的任何效果 (原理:首行读 ... -
windows下配置Apache支持PHP5
2004-09-16 17:43 16701在此之前先安装Apache然后再安装PHP5安装时选择apc ... -
搭建WAP应用开发环境 (转载)
2004-09-29 11:25 1012搭建WAP应用开发环境 ( ... -
我在思考一个问题:用纯dhtml技术来实现信息交流平台的应用.
2004-09-29 16:57 745一个偶然的时间让我这样的想法.有javascript +htm ... -
之控制表格的字数的处理方法.
2004-10-09 16:21 840<td class=v ... -
知道吗,生意通常都是这样谈成的
2005-12-16 15:14 708一位优秀的商人杰克,有一天告诉他的儿子 杰克:我已经决定好了一 ... -
当你不需要再重复以前工作的时候你该考虑做些什么?
2005-12-22 13:20 671对于我来说争取每个项目的成功施使的目的在于获得更多的项目开发经 ... -
网络就是计算机
2006-01-02 17:39 670因为一些原因机器有一两个月没有上网了,机器一直放在角落没再碰过 ... -
利用现有资源建立自己的经营模式
2006-09-05 09:45 751兴趣和爱好是一个人一生中所要经营的东西,如果能以现有的资 ... -
25年:两个错误的预言
2007-07-13 09:11 77525年:两个错误的预言 1 ...
相关推荐
"SMART 200系列地址库:灵活配置的位读写系统",SMART 200 寻址-库 6个子 位:一个读,一个写 读:例如 读取从V0.0开始的第N个位的状态 写:例如 将值写入V0.0开始的第N个位中 起始地址和第几个位都可自定义 字节:读写一体,引脚控制读或写 字:读写一体,引脚控制读或写 双字:读写一体,引脚控制读或写 实数:读写一体,引脚控制读或写 ,核心关键词:SMART 200; 寻址-库; 子位; 读; 写; 起始地址; 自定义; 字节; 字; 双字; 实数。,"SMART 200库:位寻址与多读写功能"
1、文件内容:perl-ExtUtils-Manifest-1.61-244.el7.rpm以及相关依赖 2、文件形式:tar.gz压缩包 3、安装指令: #Step1、解压 tar -zxvf /mnt/data/output/perl-ExtUtils-Manifest-1.61-244.el7.tar.gz #Step2、进入解压后的目录,执行安装 sudo rpm -ivh *.rpm 4、安装指导:私信博主,全程指导安装
多水下航行器协同定位的MATLAB仿真:基于《Cooperative Localization for Autonomous Underwater Vehicles》的研究与实践,【7】MATLAB仿真 多水下航行器协同定位,有参考文档。 主要参考文档: 1. Cooperative Localization for Autonomous Underwater Vehicles,The International Journal of Robotics Research 主要供文档方法的学习 非全文复现。 ,MATLAB仿真; 多水下航行器协同定位; 参考文档; 自主水下航行器; 机器人学国际期刊; Cooperative Localization。,MATLAB仿真:多水下航行器协同定位研究参考国际期刊论文
基于大语言模型的多模态社交媒体信息流行度预测研究
1、文件内容:perl-Algorithm-Diff-1.1902-17.el7.rpm以及相关依赖 2、文件形式:tar.gz压缩包 3、安装指令: #Step1、解压 tar -zxvf /mnt/data/output/perl-Algorithm-Diff-1.1902-17.el7.tar.gz #Step2、进入解压后的目录,执行安装 sudo rpm -ivh *.rpm 4、安装指导:私信博主,全程指导安装
《基于分时电价下电动汽车多类型充放电调度策略优化及其经济与负荷曲线改善分析》,11-分时电价下电动汽车充放电调度策略优化-100% 摘要:代码主要做的是分时电价下电动汽车充放电策略的优化,电动汽车选取了四种典型的类型,包括比亚迪EV,尼桑EV,宝马mini以及三菱EV,四种类型电动汽车取若干辆,约束重点关注充放电约束、蓄电量约束、0-1启停约束等等,目标函数为经济效益最优,同时考虑负荷曲线的改善,并通过图展示其结果,具体看下图。 ,核心关键词: 分时电价; 电动汽车; 充放电策略优化; 类型; 约束; 经济效益; 负荷曲线改善; 图展示。,电价优化下电动汽车充放电调度策略研究
IMG_20250130_120659.jpg
该项目是一款基于JavaScript、TypeScript和微信小程序开发的火车票购票系统源码,包含1634个文件,涵盖395个JavaScript文件、293个TypeScript文件、271个JSON配置文件、240个WXML模板文件、232个WXSS样式文件、138个WXS脚本文件,并辅以22个PNG图片、19个Markdown文档和1个JPG图片。该系统旨在提供便捷的火车票购票服务。
驱动DHT11要学会看数据手册
《COMSOL模拟增强型地热开采采热井温度变化一年周期的瞬态分析》,comsol增强型地热开采 本模型采用达西定律接口、多孔介质传热接口、非等温管道流接口,采用瞬态求解器,求解采热井一年的温度变化 ,comsol; 增强型地热开采; 达西定律接口; 多孔介质传热接口; 非等温管道流接口; 瞬态求解器; 采热井温度变化,《COMSOL模型在地热开采中模拟采热井一年温度变化的研究》
0016-08-16122503-曾洋.zip
1.版本:matlab2014/2019a/2024a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。
week01_lab_solutions.ipynb
免费JAVA毕业设计 2024成品源码+论文+录屏+启动教程 启动教程:https://www.bilibili.com/video/BV1SzbFe7EGZ 项目讲解视频:https://www.bilibili.com/video/BV1Tb421n72S 二次开发教程:https://www.bilibili.com/video/BV18i421i7Dx
Golang. KisFlow(Keep It Simple Flow).
免费JAVA毕业设计 2024成品源码+论文+录屏+启动教程 启动教程:https://www.bilibili.com/video/BV1SzbFe7EGZ 项目讲解视频:https://www.bilibili.com/video/BV1Tb421n72S 二次开发教程:https://www.bilibili.com/video/BV18i421i7Dx
1.版本:matlab2014/2019a/2024a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。
1、文件内容:perl-B-Keywords-1.13-2.el7.rpm以及相关依赖 2、文件形式:tar.gz压缩包 3、安装指令: #Step1、解压 tar -zxvf /mnt/data/output/perl-B-Keywords-1.13-2.el7.tar.gz #Step2、进入解压后的目录,执行安装 sudo rpm -ivh *.rpm 4、安装指导:私信博主,全程指导安装
1、文件内容:perl-Net-DNS-Nameserver-0.72-6.el7.rpm以及相关依赖 2、文件形式:tar.gz压缩包 3、安装指令: #Step1、解压 tar -zxvf /mnt/data/output/perl-Net-DNS-Nameserver-0.72-6.el7.tar.gz #Step2、进入解压后的目录,执行安装 sudo rpm -ivh *.rpm 4、安装指导:私信博主,全程指导安装
基于Comsol模拟的近场金属探针激发表面等离子体激元(SPP)的研究,Comsol近场金属探针激发SPP。 ,Comsol; 近场金属探针; SPP; 激发。,"Comsol模拟激发金属探针的SPP现象"