Skip to content

Commit ec6e156

Browse files
author
srathod
committed
- Possible fix for Multi threading blocking issue.
- JAXBContext initialization was causing the threads to block. - Added synchronized to functions getXml() and create() in XmlUtility.java. - Added two static instances of JAXBContext, one for request and one for response. - Created an on-demand cache for JAXBContext instances for each Request/Response types. The cached instance of JAXBContext will be used for new request/response in case already available.
1 parent 2feaf43 commit ec6e156

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

src/main/java/net/authorize/api/controller/base/ApiOperationBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ private void validateAndSetMerchantAuthentication() {
252252
}
253253
}
254254

255-
public synchronized String getClientId() {
255+
public String getClientId() {
256256
return Constants.CLIENT_ID;
257257
}
258258
}

src/main/java/net/authorize/util/XmlUtility.java

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.Serializable;
55
import java.io.StringReader;
66
import java.io.StringWriter;
7+
import java.util.HashMap;
78

89
import javax.xml.bind.JAXBContext;
910
import javax.xml.bind.JAXBElement;
@@ -23,13 +24,17 @@
2324
public final class XmlUtility {
2425
private static Log logger = LogFactory.getLog(XmlUtility.class);
2526
private static final String XmlHeader = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";
27+
private static JAXBContext request_ctx = null;
28+
private static JAXBContext response_ctx = null;
29+
private static HashMap<String, JAXBContext> jaxbContext = new HashMap<String, JAXBContext>();
30+
2631

2732
/**
2833
* Default C'tor, cannot be instantiated
2934
*/
3035
private XmlUtility() {
3136
}
32-
37+
3338
/**
3439
* Helper method to serialize an object to XML. Requires object to be Serializable
3540
* @param entity Object to serialize
@@ -38,15 +43,24 @@ private XmlUtility() {
3843
* @throws IOException if errors during serialization
3944
* @throws JAXBException if errors during serialization
4045
*/
41-
public static <T extends Serializable> String getXml(T entity) throws IOException, JAXBException
46+
public static synchronized <T extends Serializable> String getXml(T entity) throws IOException, JAXBException
4247
{
4348
StringWriter sw = new StringWriter();
4449

4550
if ( null != entity)
4651
{
47-
JAXBContext ctx = JAXBContext.newInstance(entity.getClass());
48-
49-
Marshaller m = ctx.createMarshaller();
52+
53+
if(!jaxbContext.containsKey(entity.getClass().toString()))
54+
{
55+
request_ctx = JAXBContext.newInstance(entity.getClass());
56+
jaxbContext.put(entity.getClass().toString(), request_ctx);
57+
}
58+
else
59+
{
60+
request_ctx = jaxbContext.get(entity.getClass().toString());
61+
}
62+
63+
Marshaller m = request_ctx.createMarshaller();
5064
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
5165

5266
m.marshal(entity, sw);
@@ -66,14 +80,23 @@ public static <T extends Serializable> String getXml(T entity) throws IOExceptio
6680
* @throws JAXBException if errors during de-serialization
6781
*/
6882
@SuppressWarnings("unchecked")
69-
public static <T extends Serializable> T create(String xml, Class<T> classType) throws JAXBException
83+
public static synchronized <T extends Serializable> T create(String xml, Class<T> classType) throws JAXBException
7084
{
7185
T entity = null;
7286
//make sure we have not null and not-empty string to de-serialize
7387
if ( null != xml && !xml.trim().isEmpty())
7488
{
75-
JAXBContext ctx = JAXBContext.newInstance(classType);
76-
Unmarshaller um = ctx.createUnmarshaller();
89+
if(!jaxbContext.containsKey(classType.toString()))
90+
{
91+
response_ctx = JAXBContext.newInstance(classType);
92+
jaxbContext.put(classType.toString(), response_ctx);
93+
}
94+
else
95+
{
96+
response_ctx = jaxbContext.get(classType.toString());
97+
}
98+
99+
Unmarshaller um = response_ctx.createUnmarshaller();
77100
try {
78101
Object unmarshaled = um.unmarshal(new StringReader(xml));
79102
if ( null != unmarshaled)

0 commit comments

Comments
 (0)