For Information on how to setup the MessageService look in the chapter "Setting up the Message Service" in the EAServer System Administration Guide.
There are 2 ways to receive messages in PB (or in Java for that matter)
1. Create a PB component that implements Listener, deploy and using MsgAdmin configure it to receive messages (so called push style)
2. Create a PB client which creates a new queue on the fly and then receive messages (so called pull style)
Common to both methods is the fact that Messages need to be put onto the messagequeue by either using the send() or publish() method. Send function send messages to specific queues directly. Publish function publish messages with topics, so queues which are connected to topics get messages by publish.
Guideline for creating a client for publishing Messages:
1. With new target build proxy project for MessageService component from ctscomponents package. Also get one connection object using connection object wizard to connect to desired EAServer.
2. Execute the proxy project and populate all the required components for the client.
3. Create a window with a
button on it. In the clicked event of the button write the following code
which publish the messages.
Example Application - A Stock Publisher MessageServerExample:CtsComponents_MessageService cms
long lRet
lRet = jagconnection.CreateInstance(cms, "CtsComponents/MessageService")
if lRet <> 0 then
MessageBox("Comp Creation Error", String(lRet))
return -1
end ifString topic = "my.topic"
String ls_text = "This is a test Message"
CtsComponents_Message msg
msg.key = cms.getMessageKey()CtsComponents_Property prop[2]
msg.props = propCtsComponents_PropertyValue pv1, pv2
pv1 = CREATE CtsComponents_PropertyValue
pv2 = CREATE CtsComponents_PropertyValueCtsComponents_Property p1, p2
p1.name = "TestName"
p1.value = pv1
msg.props[1] = p1
msg.props[1].value.stringValue("TestValue")p2.name = "Counter"
p2.value = pv2
msg.props[2] = p2
msg.props[2].value.longValue(1000)msg.replyTo = "";
msg.text = ls_text;
cms.publish(topic, msg, 0)
The MessageServer pushes
out new Messages continually to the MessageQueue. In the example the MessageServer
is a PowerBuilder program
called Stock Publisher that pushes new stockquotes via the timer event
to the MessageQueue.
Optional: MessageServer as Java program
The MessageServer is a small
multithreaded Java program (TestServer.java in zip file) that shows how
to create new Messages
and publish it to the MessageService.
Here the demo pushes out Stock values randomly.
To run the example open a command prompt and start the compiled java program : java TestServer
Now the Java Program connects
to Jaguar and pushes out stock quotes and values and prints them to the
console.
Guideline for a Message Receiver - push Style
Server Component(Listener)
:
1. Start with new target.
Select new EA Server Component wizard in Object tab. Give interface options
as Implement an existing EAServer Remote Interface. And select MessageListener
as Interface to implement from CtsComponents package. When you finish with
this wizard, powerbuilder shows MessageListener component with all dependent
components and project for deployment .
2. Edit messagelistener component
and write code in onMessage event e.g. You can write something to log into
Server log. Because when you publish or send message to queue, listener
components onMessage event get fired.
Example code in onMessage function:ErrorLogging err
GetContextService("ErrorLogging", err)err.log("PB MessageListener onMessage()")
return
3. Execute the project
and deploy listener component to EAServer.
4. Configuring Message Queue using msgadmin.bat file located at %Jaguar%\html\classes :
- add a queue to which you want to send or publish the messages. In the properties of queue add topic as selector which you are going to use for publish function.
- add listener component which you have deployed on Jaguar. eg. type PB_Messagelistener/messagelistener
Now when you run the application
for publishing messages to the queue, everytime a message comes in, the
onMessage() method
of the messagelistener component
will fire and write the text mentioned above into the log.
:
Aug 01 15:17:33 2001: PB
MessageListener onMessage()
Aug 01 15:17:43 2001: PB
MessageListener onMessage()
:
Guideline for a Message Receiver - pull
Style
1.With new target build proxy project for MessageService component from ctscomponents package. Also get one connection object using connecting object wizard to connect to desired EAServer.
2. Execute the proxy project and populate all the required components for the client.
3. Create a window with button on it. In the clicked event of the button write the following code which receives the messages.
Example:CtsComponents_MessageService cms
long lRet
lRet = jagconnection.CreateInstance(cms, "CtsComponents/MessageService")
if lRet <> 0 then
MessageBox("Comp Creation Error", String(lRet))
return -1
end ifstring _queue
_queue = cms.getUniqueName(1);ctscomponents_messagequeue msg_que
msg_que = cms.getMessageQueue(_queue,"",0)
string ls_topic="topic = 'my.topic'"
cms.addselector(_queue, ls_topic)
CtsComponents_Messageseq seq
seq = msg_que.receive(0,2)CtsComponents_Message msg1[]
For i = 1 to UpperBound(seq.item)
msg1[i] = seq.item[i]
MessageBox("Client",msg1[i].Text)
Next
Now when you run the
application for publishing messages to the queue, everytime a message comes
in when clicking the button
the Message will be shown
in a Messagebox.
Generally speaking the receive()
call has to be in a for/while loop, because there maybe times when there
are no messages and the receive call just times out.
That way you are guaranteed
to receive messages. To get out of the while loop a call to yield() enables
a user to click on a stop button.
do while k=0 //foreveryield()
seq = msg_que.receive(0,2)For i = 1 to UpperBound(seq.item)loop
msg1[i] = seq.item[i]
MessageBox("Client",msg1[i].Text)
Next
Example Application-
A Stock Message Receiver
The PB Stock Receiver is
the client program that receives stockquotes that have been pushed on the
MessageQueue by
the Stock Publisher PowerBuilder
program. The topic can be selected, so that only those messages are being
received.
The PowerBuilder client needs
to have multithreaded capabilities in order to process the asynchronous
returnvalues from
the MessageService component.
For more information about PB multithreaded application design please review
Writing MultiThreaded PowerBuilder
Applications on http://www.sybase.com/detail?id=47802
Setting up the PowerBuilder Stock Publisher and Receiver Example:
- Extract the targets into
a directory eg. C:\StockExample
- Create a new Workspace
and add the targets to it
- Start EAserver if you
haven't started it already and make sure the MessageService is correctly
setup.
- Start two instances of
PB 8 and load the pbls accordingly
- Start the PowerBuilder
Stock Publisher program - click publish stocks-button and then timer-button
to start the timer
- Start the PowerBuilder
Stock Receiver program - select a stock symbol to listen and click the
receive button
When all the steps above
have been successful we should see the Stockquote Messages received in
the PB application.

Back to Top