Thursday, February 5, 2015

Duplicate Multiple Queues and Topics to one with HermesJMS

Sometimes it is much easier to listen to only one Topic or Queue during a test. Therefore, I either use my local method that I explained in the other post, or duplicate and redirect the messages in one Queue. I'm not going to point why we should do it since there might be many different reasons. I personally prefer to use this solution to duplicate JMeter controllers and modify them with least effort. Also it is beneficial if you don't know to what topic you should listen to.

As you know, HermesJMS has it's own integrated programming interface with Jython language. I never liked the idea of combining two languages (worse than combining is using override approaches), but in this case it's mostly Python and there is no other choice.

I found some sample codes of how to do this in HermesJMS, therefore no credit goes for me.

Let's say there are Topic1 and Queue1 in session X1, and you want to forward them to an existing queue XQ in session X2 (or another Queue/Topic in X1):


Step 1- Open a text editor, modify following code and save as *.py file. You might need to modify lines 23-46 only.

from com.jidesoft.document import DocumentComponent
from com.jidesoft.document import DocumentComponentListener
from hermes.swing import SwingRunner
from javax.swing import JTextArea 
from javax.swing import JScrollPane
from java.lang import Runnable
from java.lang import Thread
from javax.jms import MessageListener
from org.apache.log4j import Logger

area = JTextArea()

class Forwarder (Runnable, DocumentComponent, DocumentComponentListener):
 def __init__(self):
  DocumentComponent.__init__(self, JScrollPane(area), "Message Forwarder")  
  self.__running = 1
  area.setColumns(80)
  area.setLineWrap(1)
  self.addDocumentComponentListener(self)  
  area.append("Ready\n")

 def run(self):
  hermesIn = browser.getContext().lookup("X1")
  hermesOut = browser.getContext().lookup("X2")  
  inbound1 = hermesIn.createTopic("Topic1")
  inbound2 = hermesIn.createQueue("Queue1")
  outbound = hermesOut.createQueue("XQ")
  
  while self.__running:
   message1 = hermesIn.receive(inbound1, 500)
   message2 = hermesIn.receive(inbound2, 500)
 
   if message1 != None:
    area.append("\nTransferred Topic1:")
    hermesOut.send(outbound, message1)  
    hermesOut.commit()  
    hermesIn.commit()
    area.append(message1.toString())
   if message2 != None:
    area.append("\nTransferred Queue1:")
    hermesOut.send(outbound, message2)  
    hermesOut.commit()  
    hermesIn.commit()
    area.append(message2.toString())
  hermesIn.close()
  hermesOut.close()

 def stop(self):
  self.__running = 0 

 def documentComponentOpened(self, event):  
  return

 def documentComponentClosing(self, event):
  return 

 def documentComponentClosed(self, event):
  self.stop()
  return

 def documentComponentMoving(self, event):
  return
  
 def documentComponentMoved(self, event):
  return

 def documentComponentActivated(self, event):
  return

 def documentComponentDeactivated(self, event):
  return
forwarder = Forwarder()
browser.addDocumentComponent(forwarder)
thread = Thread(forwarder)
thread.start()



Step 2: In your HermesJMS, open Jython tab in bottom-left corner. Then right-click and "load script" your Python file.

That's it. you will notice all incoming messages in X1 and X2 are getting duplicated into XQ