`
flowercat
  • 浏览: 358196 次
社区版块
存档分类
最新评论

get the number of online by Play

    博客分类:
  • java
阅读更多

It's not an easy problem to solve because HTTP is a stateless protocol. A
user is never really "Connected" to your application.
The best you can do is to count requests made by distinct users during the X
last minutes.

One solution is to do like Loïc shown you. But using the cache in this way
is not very safe ... be aware of races conditions ...
You can keep the Users list as a static instance of your application but it
will only work if there is only one application instance
(however it's totally acceptable for small applications ...).

But using the cache can be a very nice way to handle this requirement. The
easy way is to use a counter and to increment it for each new
user. But the difficult part is to decrement the counter when the user is
"disconnected" (because you don't really know it). Using 2 counters
you can however handle it properly and to let the Cache automatically
discard older counters ...

Something like :

    @Before
    static void countUsers() {
        // Track 2 counters of 1mn each
        Integer lastMinute = (int)System.currentTimeMillis() / 60000;

        // Counter keys
        String counter1Key = lastMinute + "";
        String counter2Key = (lastMinute-1) + "";

        // Intialize counters
        if(Cache.get(counter1Key) == null) Cache.add(counter1Key, 0L,
"2mn");
        if(Cache.get(counter2Key) == null) Cache.add(counter2Key, 0L,
"2mn");

        // Check if this user is already count
        if(!session.contains("lastCount") ||
Integer.parseInt(session.get("lastCount")) < lastMinute - 1) {
            Cache.incr(counter1Key);
            session.put("lastCount", lastMinute);
        }

        // Now count users
        Long counter1 = Cache.get(counter1Key, Long.class);
        Long counter2 = Cache.get(counter2Key, Long.class);

        // Save the result
        renderArgs.put("nbUsers", counter1 + counter2);
    }

In this way I count all users who have made a request during the last 2
minutes ...

分享到:
评论

相关推荐

    Beginning 3D Game Development with Unity 4

    Beginning 3D Game Development with Unity ...use of standard assets to investigate topics rarely covered in online tutorials or books. All of the necessary art assets to complete the project are provided.

    A Novel RFID Authentication Protocol with Ownership Transfer.doc

    The other involving a TTP relies on the security and robustness of server, which required the TTP to be online anytime. 3 Paper Preparation A novel RFID security protocol An RFID security method ...

    Beginning 3D Game Development with Unity

    You will be going beyond the basic Unity functionality and use of standard assets to investigate topics rarely covered in online tutorials or books. All of the necessary art assets to complete the ...

    王小平版遗传算法的光盘源代码

    that population, else if `none is specified on the command line, the parameters will retain the default values. Remember you must specify either a population params file or `none on the command line...

    MFC+Windows程序设计

    Later this year, I plan to post a brand new chapter on MFC DLLs that you can read and comment on online. With the huge volume of computer books vying for buyers' attention in bookstores today, I ...

    2008年6月大学英语六级A卷真题

    Turning on the same protective systems in people should, by 2056, create the first class of 100-year-olds who are as vigorous and productive as today’s people in their 60s” Aliens Colin Pillinger,...

    VC技术内幕第五版.chm

    Later this year, I plan to post a brand new chapter on MFC DLLs that you can read and comment on online. With the huge volume of computer books vying for buyers' attention in bookstores today, I ...

    Handbook of Research on Soft Computing and Nature-Inspired Algorithms

    The Handbook of Research on Soft Computing and Nature-Inspired Algorithms is an essential source for the latest scholarly research on applications of nature-inspired computing and soft computational ...

    四级英语真题

    &lt;br&gt;11. A)The girls got on well with each other. B)It's understandable that girls don't get along. C)She was angry eith the other young stars. D)The girls lacked the courage to fight....

    vs 2005 10款插件

    The goal of GhostDoc is to automate the tedious parts of writing XML comments by looking at the name of your class or method, as well as any parameters, and making an educated guess as to how the ...

    XML Processing with Perl, Python, and PHP (2002).pdf

    If I want to view the information online, can I con- vert it easily? If I build an application that provides me with access to the information, how do I go about storing my preferences? How do I make...

    CE中文版-启点CE过NP中文.exe

    Compare memory: Added a limit to the number of address values shown per row (can be changed) Address List: When the option to deactivate children is set, the children will get deactivated first Memory...

    VB编程资源大全(英文源码 控件)

    It mimics the Microsoft Outlook's Vertical Menu bar which appears on the left of the screen&lt;END&gt;&lt;br&gt;35 , progbar.zip This is a great replacement for the standard VB progress bar. Its smooth, ...

    VB编程资源大全(英文源码 网络)

    Online Weather, News headlines, Online Horoscopes, Movie Reviews etc.&lt;END&gt;&lt;br&gt;44 , TelDialOut1.zip TelDialOut is a program that dials a phone number from an application using the modem. I had ...

    ethminer-0.16.0.dev3

    For Maxwell 2 GPUs: There is a way of mining ETH at Win7/8/Linux speeds on Win10, by downgrading the GPU driver to a Win7 one (350.12 recommended) and using a build that was created using CUDA 6.5. ...

    VB编程资源大全(英文源码 其它)

    prngmit.zip This Random Number Generator is for everybody who is tired of using the "Randomize" and "Rnd" in VB and VBScript and having to watch the SAME "Random" numbers appear (The problem is ...

    apktool documentation

    The number that the framework is named during install corresponds to the pkgId of the application. These values should range from 1 to 9. Any APK that installs itself as 127 is 0x7F which is an ...

    uhdd.sys源码

    It uses only 656 to 752 bytes of upper memory depending on the system and it can also load in 640K DOS memory RDISK is a simple and small RAMdisk driver for use when resizing or other features are...

    flash action script 经典字典教程大全,学flash必备

    indexOf String.indexOf() Infinity Infinity -Infinity -Infinity INSERT Key.INSERT insertBefore XML.insertBefore() install CustomActions.install() instanceof instanceof int int interface...

    Making Games with Python & PyGame.pdf(with code)

    Checking Your Code Online ........................................................................................................ 6 More Info Links on ...

Global site tag (gtag.js) - Google Analytics