tips&tricks

Getting the depth of an MQSeries queue from Java

The JMS spec does not provide a way to easily obtain the number of messages on a certain queue. You could create a browser and browse through the entire queue and keep count, but this is very slow.

The java client for MQSeries (or WebsphereMQ as it is called nowadays) does provide this functionality, so if you don’t mind using IBM specific classes, you could do it like this:

public int getDepth(String queueName) throws MQException {
  // Build quemanager (this should be done in another method)
  // and not every time in a real life application
  MQEnvironment.channel = "CHANNELNAME";
  MQEnvironment.port = 1414;
  MQEnvironment.hostname = "qmgrhost.yourdomain.com";
  MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY, 
    MQC.TRANSPORT_MQSERIES);
  MQQueueManager qmgr = new MQQueueManager("YourQueueManagerName");
  // access the queue to query its depth
  com.ibm.mq.MQQueue queue = qmgr.accessQueue(queueName, 
    MQC.MQOO_INQUIRE | MQC.MQOO_INPUT_AS_Q_DEF, 
    null, null, null);
  return queue.getCurrentDepth();
}

The com.ibm.mq.MQQueue class exposes some other interesting methods to get more information about a queue, like the maximum depth and the maximum message length.

Custom paste behaviour for Windows Forms controls

Some Windows Forms controls come with copy/paste functionality out of the box. This is great when you just want the standard copy/paste behaviour, but what if you want to do some transformation of the data on the clipboard prior to displaying it in the Windows Forms control?

In my case I needed to convert a selection copied from Excel to a comma-delimited list of values when they were pasted in a TextBox. Since there is no OnPaste() method on the TextBox to override, I had to dig deeper to get the job done.

Usually something like this can be done by creating a subclass of the component and overriding the WndProc method to capture the right message. So I created a class called MyTextBox which extends TextBox and I trapped the windows Paste message, like this:

private const int WM_PASTE = 0x302;
 
protected override void WndProc(ref Message m)
{
    if (m.Msg == WM_PASTE)
    {
        // trigger our custom paste logic
        OnPaste();
    }
    else
    {
        base.WndProc(ref m);
    }
}
 
protected virtual void OnPaste() 
{
    // put custom logic here    
}

There you go, now my own protected virtual OnPaste() method is called when the control receives a PASTE message, and the message is not passed to the base class to prevent the default paste behavior from being executed.

All that is left to do now is get the text data from the clipboard, do my own transformations and change the Text property of the TextBox accordingly.

Continue Reading »

Sending raw data to a printer in C#

Recently I needed to send raw data containing printer-specific control characters to a serial printer. At first I started looking into a solution through direct serial communication. But since .NET 1.1 does not come with serial communication support, and I needed to access printers over a network, it seemed logical to send the data through the native Win32 Spooler API.

After some googling and surfing I ended up finding this article in the Microsoft Knowledge Base. It exactly addresses the problem I had and contains full source code for a RawPrinterHelper class.