[Web Service] Rop在进行响应或输入的序列化时存在线程安全问题
来源:互联网 发布:nginx 打印php的错误 编辑:程序博客网 时间:2024/04/27 14:03
stamen 2012-05-17
由于我原来的Marshaller是一个RopResponse对应一个实例,也即一个RopResponse类是共享一个单实例的Marshaller。当时,我认为Marshaller是线程安全的,但是经过测试,Marshaller是非线程安全的:
可以看这篇文章:
http://jaxb.java.net/guide/Performance_and_thread_safety.html
我马上会修复这个BUG的。
可以看这篇文章:
http://jaxb.java.net/guide/Performance_and_thread_safety.html
我马上会修复这个BUG的。
引用收藏
stamen 2012-05-18
RopResponseMarshaller的实现类包括以下两个:
org.codehaus.jackson.map.ObjectMapper是线程安全的(参见:http://wiki.fasterxml.com/JacksonFAQThreadSafety),而javax.xml.bind.Marshaller是非线程安全的。原来的Rop对ObjectMapper和Marshaller都采用了单实例的方式,因此存在线程安全的问题。
关于javax.xml.bind.Marshaller非线程安全的说明参见:http://jaxb.java.net/guide/Performance_and_thread_safety.html
不但javax.xml.bind.Marshaller是非线程安全的,Spring OXM的Marshaller也是非线程安全的,参见其javadoc的说明:
createMarshaller
protected Marshaller createMarshaller()
Return a newly created JAXB marshaller. JAXB marshallers are not necessarily thread safe.
原来的JaxbXmlRopResponseMarshaller:
更改为:
其实,根据测试创建Marshaller的时间很短,只有1毫秒左右,而真正慢的是创建JAXBContext,大家100毫秒左右。所以,这样改后,对Rop序列化的性能不会有什么影响,因为JAXBContext是共享的。
已经同步到git中。
- JacksonJsonRopResponseMarshaller:内部使用org.codehaus.jackson.map.ObjectMapper进行JSON序列化
- JaxbXmlRopResponseMarshaller:内部使用javax.xml.bind.Marshaller进行XML序列化
org.codehaus.jackson.map.ObjectMapper是线程安全的(参见:http://wiki.fasterxml.com/JacksonFAQThreadSafety),而javax.xml.bind.Marshaller是非线程安全的。原来的Rop对ObjectMapper和Marshaller都采用了单实例的方式,因此存在线程安全的问题。
关于javax.xml.bind.Marshaller非线程安全的说明参见:http://jaxb.java.net/guide/Performance_and_thread_safety.html
不但javax.xml.bind.Marshaller是非线程安全的,Spring OXM的Marshaller也是非线程安全的,参见其javadoc的说明:
引用
createMarshaller
protected Marshaller createMarshaller()
Return a newly created JAXB marshaller. JAXB marshallers are not necessarily thread safe.
原来的JaxbXmlRopResponseMarshaller:
- public class JaxbXmlRopResponseMarshaller implements RopResponseMarshaller {
- private static Map<Class, Marshaller> marshallerMap = new HashMap<Class, Marshaller>();
- public void marshaller(RopResponse response, OutputStream outputStream) {
- try {
- Marshaller m = getMarshaller(response);
- m.marshal(response, outputStream);
- } catch (JAXBException e) {
- throw new RopException(e);
- }
- }
- private Marshaller getMarshaller(RopResponse response) throws JAXBException {
- if (!marshallerMap.containsKey(response.getClass())) {
- JAXBContext context = JAXBContext.newInstance(response.getClass());
- Marshaller marshaller = context.createMarshaller();//Marshaller是非线程安全的,不能多线程共用 !!!!
- marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
- marshaller.setProperty(Marshaller.);
- marshaller.setProperty(Marshaller.JAXB_ENCODING, "utf-8");
- marshallerMap.put(response.getClass(), marshaller);
- }
- return marshallerMap.get(response.getClass());
- }
- }
更改为:
- public class JaxbXmlRopResponseMarshaller implements RopResponseMarshaller {
- private static Map<Class, JAXBContext> jaxbContextHashMap = new ConcurrentHashMap<Class, JAXBContext>();
- public void marshaller(RopResponse response, OutputStream outputStream) {
- try {
- Marshaller m = buildMarshaller(response);
- m.marshal(response, outputStream);
- } catch (JAXBException e) {
- throw new RopException(e);
- }
- }
- public Marshaller buildMarshaller(RopResponse response) throws JAXBException {
- if (!jaxbContextHashMap.containsKey(response.getClass())) {
- JAXBContext context = JAXBContext.newInstance(response.getClass());//JAXBContext是线程安全的
- jaxbContextHashMap.put(response.getClass(), context);
- }
- JAXBContext context = jaxbContextHashMap.get(response.getClass());
- Marshaller marshaller = context.createMarshaller();//每次创建一个新的Marshaller
- marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
- marshaller.setProperty(Marshaller.JAXB_ENCODING, "utf-8");
- return marshaller;
- }
- }
其实,根据测试创建Marshaller的时间很短,只有1毫秒左右,而真正慢的是创建JAXBContext,大家100毫秒左右。所以,这样改后,对Rop序列化的性能不会有什么影响,因为JAXBContext是共享的。
已经同步到git中。
- [Web Service] Rop在进行响应或输入的序列化时存在线程安全问题
- MRP、ROP、JIT系统进行重复性生产存在的主要问题
- .net 调用web Service时集合序列化的解决方案
- Java Web并发访问的线程安全问题
- Java Web并发访问的线程安全问题
- axis下web service 在weblogic安全问题解决
- Rop特性及新功能(最简单的Rest Web Service框架)
- web开发中存在的安全问题及防范
- ffmpeg的avcodec_open或avcodec_open2()的线程安全问题
- Ajax访问Xml Web Service的安全问题以及解决方案
- 禁止在窗口界面线程中进行线程同步或进行耗时的操作。。。
- SimpleDateFormat时间格式化存在线程安全问题
- SimpleDateFormat时间格式化存在线程安全问题
- 在使用mybatis时进行批删除或批添加用到的service层事务的理解
- 构造函数线程安全-即dubbo的extensionLoader存在线程安全问题
- java web app 线程安全问题
- Provider存在的一些安全问题
- Web服务器开发环境下的线程安全问题
- 句柄转控件
- Entityframework乱码
- 使用skin++后如何利用GetMenu获取菜单指针
- 判断奇偶数
- Android中集成第三方软件包(.jar, .so)
- [Web Service] Rop在进行响应或输入的序列化时存在线程安全问题
- 深入理解javascript闭包(转)
- 基本数据类型的自动封装与拆箱
- Gesture Recognizers与触摸事件分发
- Webdriver学习笔记(一)浅谈selenium 2.0和安装
- ORA-08002: sequence TEST1.CURRVAL is not yet defined in this session
- WEB前端开发 - 专注前端开发,关注用户体验
- C++文件操作详解
- ubuntu下创建eclipse快捷方式