ARTEMIS-1666 refactor to avoid duplicate code and fix bug

This commit is contained in:
Justin Bertram 2018-02-12 13:18:57 -06:00 committed by Clebert Suconic
parent d83758e1aa
commit c26c970ddc
5 changed files with 39 additions and 109 deletions

View File

@ -16,11 +16,7 @@
*/
package org.apache.activemq.artemis.jms.server.impl;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import javax.naming.NamingException;
import javax.transaction.xa.Xid;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
@ -28,14 +24,11 @@ import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
@ -64,9 +57,6 @@ import org.apache.activemq.artemis.core.server.management.Notification;
import org.apache.activemq.artemis.core.server.reload.ReloadCallback;
import org.apache.activemq.artemis.core.server.reload.ReloadManager;
import org.apache.activemq.artemis.core.settings.impl.AddressSettings;
import org.apache.activemq.artemis.core.transaction.ResourceManager;
import org.apache.activemq.artemis.core.transaction.Transaction;
import org.apache.activemq.artemis.core.transaction.TransactionDetail;
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
import org.apache.activemq.artemis.jms.client.ActiveMQDestination;
import org.apache.activemq.artemis.jms.client.ActiveMQQueue;
@ -90,7 +80,6 @@ import org.apache.activemq.artemis.jms.server.config.impl.FileJMSConfiguration;
import org.apache.activemq.artemis.jms.server.management.JMSNotificationType;
import org.apache.activemq.artemis.jms.transaction.JMSTransactionDetail;
import org.apache.activemq.artemis.spi.core.naming.BindingRegistry;
import org.apache.activemq.artemis.utils.JsonLoader;
import org.apache.activemq.artemis.utils.SelectorTranslator;
import org.apache.activemq.artemis.utils.TimeAndCounterIDGenerator;
import org.apache.activemq.artemis.utils.XMLUtil;
@ -1337,104 +1326,12 @@ public class JMSServerManagerImpl implements JMSServerManager, ActivateCallback
@Override
public String listPreparedTransactionDetailsAsJSON() throws Exception {
ResourceManager resourceManager = server.getResourceManager();
Map<Xid, Long> xids = resourceManager.getPreparedTransactionsWithCreationTime();
if (xids == null || xids.size() == 0) {
return "";
}
ArrayList<Entry<Xid, Long>> xidsSortedByCreationTime = new ArrayList<>(xids.entrySet());
Collections.sort(xidsSortedByCreationTime, new Comparator<Entry<Xid, Long>>() {
@Override
public int compare(final Entry<Xid, Long> entry1, final Entry<Xid, Long> entry2) {
// sort by creation time, oldest first
return (int) (entry1.getValue() - entry2.getValue());
}
});
JsonArrayBuilder txDetailListJson = JsonLoader.createArrayBuilder();
for (Map.Entry<Xid, Long> entry : xidsSortedByCreationTime) {
Xid xid = entry.getKey();
Transaction tx = resourceManager.getTransaction(xid);
if (tx == null) {
continue;
}
TransactionDetail detail = new JMSTransactionDetail(xid, tx, entry.getValue());
txDetailListJson.add(detail.toJSON());
}
return txDetailListJson.toString();
return server.getActiveMQServerControl().listPreparedTransactionDetailsAsJSON((xid, tx, creation) -> new JMSTransactionDetail(xid, tx, creation));
}
@Override
public String listPreparedTransactionDetailsAsHTML() throws Exception {
ResourceManager resourceManager = server.getResourceManager();
Map<Xid, Long> xids = resourceManager.getPreparedTransactionsWithCreationTime();
if (xids == null || xids.size() == 0) {
return "<h3>*** Prepared Transaction Details ***</h3><p>No entry.</p>";
}
ArrayList<Entry<Xid, Long>> xidsSortedByCreationTime = new ArrayList<>(xids.entrySet());
Collections.sort(xidsSortedByCreationTime, new Comparator<Entry<Xid, Long>>() {
@Override
public int compare(final Entry<Xid, Long> entry1, final Entry<Xid, Long> entry2) {
// sort by creation time, oldest first
return (int) (entry1.getValue() - entry2.getValue());
}
});
StringBuilder html = new StringBuilder();
html.append("<h3>*** Prepared Transaction Details ***</h3>");
for (Map.Entry<Xid, Long> entry : xidsSortedByCreationTime) {
Xid xid = entry.getKey();
Transaction tx = resourceManager.getTransaction(xid);
if (tx == null) {
continue;
}
TransactionDetail detail = new JMSTransactionDetail(xid, tx, entry.getValue());
JsonObject txJson = detail.toJSON();
html.append("<table border=\"1\">");
html.append("<tr><th>creation_time</th>");
html.append("<td>" + txJson.get(TransactionDetail.KEY_CREATION_TIME) + "</td>");
html.append("<th>xid_as_base_64</th>");
html.append("<td colspan=\"3\">" + txJson.get(TransactionDetail.KEY_XID_AS_BASE64) + "</td></tr>");
html.append("<tr><th>xid_format_id</th>");
html.append("<td>" + txJson.get(TransactionDetail.KEY_XID_FORMAT_ID) + "</td>");
html.append("<th>xid_global_txid</th>");
html.append("<td>" + txJson.get(TransactionDetail.KEY_XID_GLOBAL_TXID) + "</td>");
html.append("<th>xid_branch_qual</th>");
html.append("<td>" + txJson.get(TransactionDetail.KEY_XID_BRANCH_QUAL) + "</td></tr>");
html.append("<tr><th colspan=\"6\">Message List</th></tr>");
html.append("<tr><td colspan=\"6\">");
html.append("<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">");
JsonArray msgs = txJson.getJsonArray(TransactionDetail.KEY_TX_RELATED_MESSAGES);
for (int i = 0; i < msgs.size(); i++) {
JsonObject msgJson = msgs.getJsonObject(i);
JsonObject props = msgJson.getJsonObject(TransactionDetail.KEY_MSG_PROPERTIES);
StringBuilder propstr = new StringBuilder();
for (String key : props.keySet()) {
propstr.append(key);
propstr.append("=");
propstr.append(props.get(key));
propstr.append(", ");
}
html.append("<th>operation_type</th>");
html.append("<td>" + msgJson.get(TransactionDetail.KEY_MSG_OP_TYPE) + "</th>");
html.append("<th>message_type</th>");
html.append("<td>" + msgJson.get(TransactionDetail.KEY_MSG_TYPE) + "</td></tr>");
html.append("<tr><th>properties</th>");
html.append("<td colspan=\"3\">" + propstr.toString() + "</td></tr>");
}
html.append("</table></td></tr>");
html.append("</table><br/>");
}
return html.toString();
return server.getActiveMQServerControl().listPreparedTransactionDetailsAsHTML((xid, tx, creation) -> new JMSTransactionDetail(xid, tx, creation));
}
// Public --------------------------------------------------------

View File

@ -32,7 +32,7 @@ import org.apache.activemq.artemis.jms.client.ActiveMQTextMessage;
public class JMSTransactionDetail extends TransactionDetail {
public JMSTransactionDetail(Xid xid, Transaction tx, Long creation) throws Exception {
public JMSTransactionDetail(Xid xid, Transaction tx, Long creation) {
super(xid, tx, creation);
}

View File

@ -114,6 +114,7 @@ import org.apache.activemq.artemis.core.settings.impl.SlowConsumerPolicy;
import org.apache.activemq.artemis.core.transaction.ResourceManager;
import org.apache.activemq.artemis.core.transaction.Transaction;
import org.apache.activemq.artemis.core.transaction.TransactionDetail;
import org.apache.activemq.artemis.core.transaction.TransactionDetailFactory;
import org.apache.activemq.artemis.core.transaction.impl.CoreTransactionDetail;
import org.apache.activemq.artemis.core.transaction.impl.XidImpl;
import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection;
@ -1241,6 +1242,10 @@ public class ActiveMQServerControlImpl extends AbstractControl implements Active
@Override
public String listPreparedTransactionDetailsAsJSON() throws Exception {
return listPreparedTransactionDetailsAsJSON((xid, tx, creation) -> new CoreTransactionDetail(xid, tx, creation));
}
public String listPreparedTransactionDetailsAsJSON(TransactionDetailFactory factory) throws Exception {
checkStarted();
clearIO();
@ -1269,7 +1274,7 @@ public class ActiveMQServerControlImpl extends AbstractControl implements Active
continue;
}
TransactionDetail detail = new CoreTransactionDetail(xid, tx, entry.getValue());
TransactionDetail detail = factory.createTransactionDetail(xid, tx, entry.getValue());
txDetailListJson.add(detail.toJSON());
}
@ -1281,6 +1286,10 @@ public class ActiveMQServerControlImpl extends AbstractControl implements Active
@Override
public String listPreparedTransactionDetailsAsHTML() throws Exception {
return listPreparedTransactionDetailsAsHTML((xid, tx, creation) -> new CoreTransactionDetail(xid, tx, creation));
}
public String listPreparedTransactionDetailsAsHTML(TransactionDetailFactory factory) throws Exception {
checkStarted();
clearIO();
@ -1311,7 +1320,7 @@ public class ActiveMQServerControlImpl extends AbstractControl implements Active
continue;
}
TransactionDetail detail = new CoreTransactionDetail(xid, tx, entry.getValue());
TransactionDetail detail = factory.createTransactionDetail(xid, tx, entry.getValue());
JsonObject txJson = detail.toJSON();

View File

@ -0,0 +1,24 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.artemis.core.transaction;
import javax.transaction.xa.Xid;
public interface TransactionDetailFactory {
TransactionDetail createTransactionDetail(Xid xid, Transaction tx, Long creation);
}

View File

@ -26,7 +26,7 @@ import org.apache.activemq.artemis.core.transaction.TransactionDetail;
public class CoreTransactionDetail extends TransactionDetail {
public CoreTransactionDetail(Xid xid, Transaction tx, Long creation) throws Exception {
public CoreTransactionDetail(Xid xid, Transaction tx, Long creation) {
super(xid, tx, creation);
}