AMQ-7467 - A new XStream serializer is created on every message that flows through the broker causing high CPU utilization under load (#523)

[AMQ-7467] Reuse xstream serializer to reduce CPU usage.

Co-authored-by: murali.mogalayapalli <Murali.Mogalayapalli@tylertech.com>
This commit is contained in:
thodimi1 2020-05-19 12:51:38 -04:00 committed by GitHub
parent e13abcf523
commit 0c00e1b791
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 2 deletions

View File

@ -59,6 +59,7 @@ import com.thoughtworks.xstream.io.xml.xppdom.XppFactory;
public class JmsFrameTranslator extends LegacyFrameTranslator implements BrokerContextAware { public class JmsFrameTranslator extends LegacyFrameTranslator implements BrokerContextAware {
XStream xStream = null; XStream xStream = null;
XStream xStreamAdvisory = null;
BrokerContext brokerContext; BrokerContext brokerContext;
@Override @Override
@ -218,12 +219,19 @@ public class JmsFrameTranslator extends LegacyFrameTranslator implements BrokerC
out = new PrettyPrintWriter(buffer); out = new PrettyPrintWriter(buffer);
} }
XStream xstream = getXStream(); XStream xstream = getXStreamAdvisory();
xstream.setMode(XStream.NO_REFERENCES); xstream.setMode(XStream.NO_REFERENCES);
xstream.aliasPackage("", "org.apache.activemq.command"); xstream.aliasPackage("", "org.apache.activemq.command");
xstream.marshal(ds, out); xstream.marshal(ds, out);
return buffer.toString(); return buffer.toString();
} }
private XStream getXStreamAdvisory()
{
if (xStreamAdvisory == null) {
xStreamAdvisory = createXStream();
}
return xStreamAdvisory;
}
// Properties // Properties
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------

View File

@ -125,6 +125,8 @@ public class ProtocolConverter {
private int lastCommandId; private int lastCommandId;
private final AtomicBoolean connected = new AtomicBoolean(false); private final AtomicBoolean connected = new AtomicBoolean(false);
private final FrameTranslator frameTranslator = new LegacyFrameTranslator(); private final FrameTranslator frameTranslator = new LegacyFrameTranslator();
private ConcurrentMap<String, FrameTranslator> jmsFrameTranslators=new ConcurrentHashMap<String,FrameTranslator>();
private final FactoryFinder FRAME_TRANSLATOR_FINDER = new FactoryFinder("META-INF/services/org/apache/activemq/transport/frametranslator/"); private final FactoryFinder FRAME_TRANSLATOR_FINDER = new FactoryFinder("META-INF/services/org/apache/activemq/transport/frametranslator/");
private final BrokerContext brokerContext; private final BrokerContext brokerContext;
private String version = "1.0"; private String version = "1.0";
@ -189,7 +191,17 @@ public class ProtocolConverter {
FrameTranslator translator = frameTranslator; FrameTranslator translator = frameTranslator;
try { try {
if (header != null) { if (header != null) {
translator = (FrameTranslator) FRAME_TRANSLATOR_FINDER.newInstance(header); translator=jmsFrameTranslators.get(header);
if(translator==null) {
LOG.info("Creating a new FrameTranslator to convert "+header);
translator = (FrameTranslator) FRAME_TRANSLATOR_FINDER.newInstance(header);
if(translator!=null) {
LOG.info("Created a new FrameTranslator to convert "+header);
jmsFrameTranslators.put(header,translator);
}else {
LOG.error("Failed in creating FrameTranslator to convert "+header);
}
}
} else { } else {
if (destination != null && (advisory || AdvisorySupport.isAdvisoryTopic(destination))) { if (destination != null && (advisory || AdvisorySupport.isAdvisoryTopic(destination))) {
translator = new JmsFrameTranslator(); translator = new JmsFrameTranslator();
@ -197,6 +209,8 @@ public class ProtocolConverter {
} }
} catch (Exception ignore) { } catch (Exception ignore) {
// if anything goes wrong use the default translator // if anything goes wrong use the default translator
LOG.debug("Failed in getting a FrameTranslator to convert ", ignore);
translator = frameTranslator;
} }
if (translator instanceof BrokerContextAware) { if (translator instanceof BrokerContextAware) {