JSP 标记类中的同步块
在过去的几天里,我试图找到以下问题的答案,但找不到全面的答案
问题陈述
我有一个自定义的JSP标签类,它处理Web表单提交,捕获数据并写入它到文件系统中的同一文件。 与所有 Web 应用程序一样,这可以同时触发,我担心多个线程会同时处理每个提交(我们都知道 Servlet 的工作原理。)
代码
synchronized (this){
final String reportFileName = "testReport.csv";
File reportDir = new File( rootCsDirectory, "reports" );
if(!reportDir.isDirectory())reportDir.mkdir();
File reportFile = new File (reportDir, reportFileName);
logReport(reportFile,reportContent.toString());
}
问题: - File 对象可以由一个线程打开进行写入,同时另一个线程可能会尝试访问并失败并引发异常 所以我认为同步(在对象上)应该解决这个问题,但是阅读了一些 jsp 引擎将具有 jsp 标记对象池的内容,所以我担心 synchronized (this) 不起作用,应该更改为 同步(this.getClass()) 仅供参考:上面的代码放置在 JSP 自定义标记类中。
编辑:
问题 1: 代码块应该通过 synchronized (this) 同步还是 synchronized (this.getClass()) 同步
问题2:同样的场景,如果Web应用部署在集群环境中,会如何处理?
I am trying to find answer for the following ,for the past couple of days ,but couldnt find comprehensive answer
Problem Statement
I have a custom JSP tag class which handles a web form submission ,captures data and write it to same file in the filesystem.
As all web applications,this can be triggeredsimultaneosly ,and i fear that multiple threads would be in action handling each of the submission (we all know thats how Servlet works.)
CODE
synchronized (this){
final String reportFileName = "testReport.csv";
File reportDir = new File( rootCsDirectory, "reports" );
if(!reportDir.isDirectory())reportDir.mkdir();
File reportFile = new File (reportDir, reportFileName);
logReport(reportFile,reportContent.toString());
}
ISSUE:
- A File object can be opened by one thread for writing and at same time another thread might try to access and fail and throw an exception
So i thought of synchronizing (on the object ) should solve the issue , but read some where that jsp engine would have pool of jsp tag objects, so i am afraid that
synchronized (this) wont work and it should be changed to
synchronized (this.getClass())
FYI: The code above is placed in JSP Custom Tag Class.
EDIT:
Question 1: should the block of code be synchronized by synchronized (this) OR synchronized (this.getClass())
Question 2: How the same scenario would be handled if the web application is deployed in the clustered environment ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会在更精细的级别上进行同步,并选择与文件创建更紧密相关的对象。
例如,将上述内容抽象为一个 FileManager 类,并让该实例的一个实例在锁对象上同步(在 FileManager 内部保存) -也许它可以锁定自己)。
这样,您就可以更精细地控制同步,并且可以更好地控制要锁定的对象。它们不受您的 servlet/web 容器控制。
I would synchronise at a finer level, and choose an object that is more tightly tied to the file creation.
e.g. abstract out the above into a
FileManager
class, and let a single instance of that synchronise on a lock object (held internally to theFileManager
- it could lock on itself, perhaps).That way you're controlling the synchronisation at a finer level, and you have more control of the objects that you're locking on. They're not controlled by your servlet/web container.
我会检查 java.util.concurrent 包中的内容。
您可以使用并发队列来推送更新,并在另一端使用应用程序启动时生成的线程来写入文件系统。
这将很好且高效地序列化对该文件的访问。
I would check out the stuff in the java.util.concurrent package.
You can use a cuncurrent queue to push your updates and have at the other side a thread you spawned when the application started which does the writes to the file system.
This will nicely and efficiently serialize access to that file.
将其放入同步方法中怎么样,这样您就可以愉快地忽略 JSP 管理方式的细节?
How about putting it in a synchronized method so you can cheerfully disregard the detail of how JSPs are managed?