Jython and cMsg

From GlueXWiki
Jump to: navigation, search

Example Jython script that sends a cMsg message:

import time


# append cMsg jar file to classpath
import sys
sys.path.append("/group/da/ejw/cMsg-3.3/build/lib/cMsg.jar")


# import cMsg classes
from org.jlab.coda.cMsg import *


# create cmsg object and connect
c = cMsg("cMsg://localhost/cMsg","fred","test sender")
c.connect()


# create and fill cMsg message
m = cMsgMessage()
m.setSubject("fred")
m.setType("test")
m.setText("hello world")


# send message and sleep a bit
c.send(m)
time.sleep(2)



Example Jython script that receives a cMsg message:

import time


# append cMsg jar file to classpath
import sys
sys.path.append("/group/da/ejw/cMsg-3.3/build/lib/cMsg.jar")


# import cMsg classes
from org.jlab.coda.cMsg import *


# create callback class
class cb(cMsgCallbackAdapter):
    def callback(self,m,o):
        print "I got a message from " + m.getSubject() + " with text " + m.getText()
        return



# create cmsg object and connect
c = cMsg("cMsg://localhost/cMsg","arty","test sender")
c.connect()


# subscribe and start receiving
c.subscribe("fred", "*", cb(), 0)
c.start()


# wait for messages to arrive
while (1==1):
    time.sleep(1)