I am writing a simple Java Swing utility that will read messages from an MQ JMS server and display them in a JTable.
private void getMessages() {
try {
if (null != Queue) {
Queue.close(); //Close previous queue connection if there is one.
}
Queue = new MQQueue(QueueManager, tableQueues.getValueAt(tableQueues.getSelectedRow(), 1).toString(), MQConstants.MQOO_INPUT_SHARED | MQConstants.MQOO_BROWSE | MQConstants.MQOO_OUTPUT, queueManager, null, null);
int count = 0;
modelMessages.setRowCount(0);
MQGetMessageOptions getOptions = new MQGetMessageOptions();
getOptions.options = MQConstants.MQGMO_BROWSE_FIRST;
ArrayList<Object[]> rows = new ArrayList<Object[]>();
while(true) {
if (count > 0) {
getOptions.options = MQConstants.MQGMO_BROWSE_NEXT;
}
MQMessage message = new MQMessage();
try {
Queue.get(message, getOptions);
byte[] b = new byte[message.getMessageLength()];
message.readFully(b);
rows.add(new Object[]{count + 1, new String(b)});
modelMessages.addRow(new Object[]{count + 1, new String(b)});
message.clearMessage();
count++;
}
catch (IOException e) {
break;
}
catch (MQException e) {
if (e.completionCode == 2 && e.reasonCode == MQConstants.MQRC_NO_MSG_AVAILABLE) {
break;
}
}
}
modelMessages.fireTableDataChanged();
} catch (MQException e) {
txtMessage.setText("MQJE001: Completion Code '" + e.completionCode + "', Reason '" + e.reasonCode + "'.");
modelMessages.setRowCount(0);
modelMessages.fireTableDataChanged();
}
}
This works well for smaller queues but for large queues, it takes a while to populate this table and in the meantime, the Swing application is frozen so I am investigating ways to populate the JTable in the background while keeping not only the application but the JTable itself usable and scrollable during this.
I'm not very familiar with threading and I've tried a few things such wrapping some parts in SwingUtilities.invokeLater and implementing doInBackground() but nothing has seemed to work. Could someone please point me in the right direction of how to proceed with this?
EDIT
Based on the replies below, here is the solution:
public class GetMessagesWorker extends SwingWorker<DefaultTableModel, Object[]> {
//http://stackoverflow.com/questions/22072480/java-updating-jtable-with-lots-of-rows-in-the-background#
private final DefaultTableModel model;
public GetMessagesWorker(DefaultTableModel model){
this.model = model;
}
@Override
protected DefaultTableModel doInBackground() throws Exception {
try {
if (null != Queue) {
Queue.close(); //Close previous queue connection if there is one.
}
Queue = new MQQueue(QueueManager, tableQueues.getValueAt(tableQueues.getSelectedRow(), 1).toString(), MQConstants.MQOO_INPUT_SHARED | MQConstants.MQOO_BROWSE | MQConstants.MQOO_OUTPUT, queueManager, null, null);
int count = 0;
modelMessages.setRowCount(0);
MQGetMessageOptions getOptions = new MQGetMessageOptions();
getOptions.options = MQConstants.MQGMO_BROWSE_FIRST;
while(true) {
if (count > 0) {
getOptions.options = MQConstants.MQGMO_BROWSE_NEXT;
}
MQMessage message = new MQMessage();
try {
Queue.get(message, getOptions);
byte[] b = new byte[message.getMessageLength()];
message.readFully(b);
Object[] row = {count + 1, new String(b)};
publish(row);
message.clearMessage();
count++;
if (isCancelled()) {
modelMessages.setRowCount(0);
count = 0;
message.clearMessage();
return model;
}
}
catch (IOException e) {
break;
}
catch (MQException e) {
if (e.completionCode == 2 && e.reasonCode == MQConstants.MQRC_NO_MSG_AVAILABLE) {
break;
}
}
}
//modelMessages.fireTableDataChanged();
} catch (MQException e) {
txtMessage.setText("MQJE001: Completion Code '" + e.completionCode + "', Reason '" + e.reasonCode + "'.");
modelMessages.setRowCount(0);
modelMessages.fireTableDataChanged();
}
return model;
}
@Override
protected void process(List<Object[]> chunks){
for(Object[] row : chunks){
model.addRow(row);
}
}
}
And here is the listener:
tableQueues = new JTable(modelQueues);
tableQueues.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
txtMessage.setText("");
if (tableQueues.getSelectedRow() > -1) {
gmw.cancel(false);
gmw = new GetMessagesWorker(modelMessages);
gmw.execute();
}
}
}
});