如何在服务器端自动执行功能

时间:2022-04-22 16:01:36

Recently I was creating an auction site. I want to make it like when user bid the item, there is a AI bidder to upbid the user. For say user bid on item1 after 5 seconds the AI bidder will auto bid the item1 as well. Any idea how can I execute it automatically after 5 seconds?

最近我在创建一个拍卖网站。我想让它像用户出价项目一样,有一个AI出价者来提高用户价格。对于用户在5秒后对item1的出价,AI出价者也会自动出价item1。知道如何在5秒后自动执行它?

4 个解决方案

#1


0  

A simple and efficient solution could be to store all future bids with a "due date" and all the information to bid in a list. Then every 5 seconds or so you could loop through the list and make all bids if they are due. This system would be extensible and would work for a large amount of bids. Of course, ideally this would run in a different thread.

一个简单而有效的解决方案可能是将所有未来的出价与“截止日期”存储在一起,并将所有信息存储在列表中。然后,每隔5秒左右,您就可以遍历列表,并在所有出价到期时进行。该系统是可扩展的,可用于大量出价。当然,理想情况下,这将在不同的线程中运行。

It's a bit like re-implementing a "cron-like" job management in your servlet but I can't see any solution that would fit your needs out of the box.

这有点像在servlet中重新实现“类似cron”的作业管理,但我看不到任何符合您需求的解决方案。

I am not sure I answered your question, hope so.

我不确定我回答了你的问题,希望如此。

Regards, Stéphane

#2


0  

Depends on what technology you actually use, you can use EJB timers for that for example, just start the timer ejb when a new bid occurs, on timer timeout (after some time) the method executes and updates the bid.

取决于您实际使用的技术,您可以使用EJB计时器,例如,在新的出价发生时启动计时器ejb,在计时器超时(超过一段时间后)方法执行并更新出价。

#3


0  

Standard servlet solution

标准servlet解决方案

  1. Create a Filter, map it to the url pattern of your bid servlet.
  2. 创建过滤器,将其映射到出价servlet的url模式。

  3. In your doFilter(), after your filterChain.doFilter() call (ie, after the request has been processed by the servlet/JSP), schedule an action for 5 seconds in the future (you can use the standard java ScheduledExecutorService)
  4. 在你的doFilter()中,在你的filterChain.doFilter()调用之后(即,在servlet / JSP处理了请求之后),将来安排一个5秒的动作(你可以使用标准的java ScheduledExecutorService)

  5. In the Runnable implementation you schedule (your task), place the AI bid.
  6. 在您安排的Runnable实施(您的任务)中,放置AI出价。

#4


0  

In my opinion:

在我看来:

  1. If user bid, and after 5 secs, it sends the request to the server, i prefer JS with setTimeout(). (Of course it required Browser's JS - read more abt this in W3School).
  2. 如果用户出价,并且在5秒后,它将请求发送到服务器,我更喜欢JS和setTimeout()。 (当然它需要浏览器的JS - 在W3School中阅读更多内容)。

  3. Otherwise, you can use an array (or smt like that) act as an queue (in server side), after each 5 secs, it's lock the queue (sync), and check for which inserted 5 secs ago, and process it (or use an Thread for each time an event requests to server). Basically, you can use a thread to do that trick? (Did u mean this?).
  4. 否则,您可以使用一个数组(或类似的smt)充当队列(在服务器端),每隔5秒后,它会锁定队列(同步),并检查5秒前插入的数据并处理它(或者每次事件请求服务器时使用一个线程)。基本上,你可以使用一个线程来做这个伎俩? (你是说这个吗?)。

#1


0  

A simple and efficient solution could be to store all future bids with a "due date" and all the information to bid in a list. Then every 5 seconds or so you could loop through the list and make all bids if they are due. This system would be extensible and would work for a large amount of bids. Of course, ideally this would run in a different thread.

一个简单而有效的解决方案可能是将所有未来的出价与“截止日期”存储在一起,并将所有信息存储在列表中。然后,每隔5秒左右,您就可以遍历列表,并在所有出价到期时进行。该系统是可扩展的,可用于大量出价。当然,理想情况下,这将在不同的线程中运行。

It's a bit like re-implementing a "cron-like" job management in your servlet but I can't see any solution that would fit your needs out of the box.

这有点像在servlet中重新实现“类似cron”的作业管理,但我看不到任何符合您需求的解决方案。

I am not sure I answered your question, hope so.

我不确定我回答了你的问题,希望如此。

Regards, Stéphane

#2


0  

Depends on what technology you actually use, you can use EJB timers for that for example, just start the timer ejb when a new bid occurs, on timer timeout (after some time) the method executes and updates the bid.

取决于您实际使用的技术,您可以使用EJB计时器,例如,在新的出价发生时启动计时器ejb,在计时器超时(超过一段时间后)方法执行并更新出价。

#3


0  

Standard servlet solution

标准servlet解决方案

  1. Create a Filter, map it to the url pattern of your bid servlet.
  2. 创建过滤器,将其映射到出价servlet的url模式。

  3. In your doFilter(), after your filterChain.doFilter() call (ie, after the request has been processed by the servlet/JSP), schedule an action for 5 seconds in the future (you can use the standard java ScheduledExecutorService)
  4. 在你的doFilter()中,在你的filterChain.doFilter()调用之后(即,在servlet / JSP处理了请求之后),将来安排一个5秒的动作(你可以使用标准的java ScheduledExecutorService)

  5. In the Runnable implementation you schedule (your task), place the AI bid.
  6. 在您安排的Runnable实施(您的任务)中,放置AI出价。

#4


0  

In my opinion:

在我看来:

  1. If user bid, and after 5 secs, it sends the request to the server, i prefer JS with setTimeout(). (Of course it required Browser's JS - read more abt this in W3School).
  2. 如果用户出价,并且在5秒后,它将请求发送到服务器,我更喜欢JS和setTimeout()。 (当然它需要浏览器的JS - 在W3School中阅读更多内容)。

  3. Otherwise, you can use an array (or smt like that) act as an queue (in server side), after each 5 secs, it's lock the queue (sync), and check for which inserted 5 secs ago, and process it (or use an Thread for each time an event requests to server). Basically, you can use a thread to do that trick? (Did u mean this?).
  4. 否则,您可以使用一个数组(或类似的smt)充当队列(在服务器端),每隔5秒后,它会锁定队列(同步),并检查5秒前插入的数据并处理它(或者每次事件请求服务器时使用一个线程)。基本上,你可以使用一个线程来做这个伎俩? (你是说这个吗?)。