I have Flex 3.6A + BlazeDS + java6 webapp.
I want to push message from server that must be intercepted by the client (.mxml page). Googlin around I manage to build the follow example:
public class TestMessaging extends ServiceAdapter {
private String previousMsg = "";
private volatile boolean running;
private Message createTestMessage() {
AsyncMessage msg = new AsyncMessage();
msg.setDestination("RandomDataPush");
msg.setClientId(UUIDUtils.createUUID());
msg.setMessageId(UUIDUtils.createUUID());
msg.setBody(BarValues.STATUS);
return msg;
}
@Override
public void start(){
super.start();
Thread messageSender = new Thread(){
public void run(){
running = true;
while(running){
sendMessageToClients(createTestMessage());
secondsToSleep(3);
}
}
};
messageSender.start();
}
/**
* @see flex.messaging.services.ServiceAdapter#stop()
*/
@Override
public void stop(){
super.stop();
running = false;
}
public void sendMessageToClients(Message msg) {
if (!msg.getBody().equals(previousMsg)) {
previousMsg = msg.getBody().toString();
((MessageService) getDestination().getService()).pushMessageToClients(msg, false);
}
}
@Override
public Object invoke(Message message) {
if (message.getBody().equals("stop")) {
running = false;
} else if (message.getBody().equals("start")) {
start();
}
return null;
}
private void secondsToSleep(int seconds) {
try{
Thread.sleep(seconds * 1000);
}catch(InterruptedException e){
System.out.println("TestServiceAdapter Interrupted while sending messages");
e.printStackTrace();
}
}
}
In the .mxml I have:
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="100%" height="100%" creationComplete="init()">
<mx:Script>
<![CDATA[
private function messageHandler(message:IMessage):void {
randomNumbers = message.body as String;
if (randomNumbers == null) {
var msg:AsyncMessage = new AsyncMessage();
msg.body = "stop";
producer.send(msg);
} else Alert.show(randomNumbers.toString());
}
private function init():void {
var msg:AsyncMessage = new AsyncMessage();
msg.body = "stop";
producer.send(msg);
consumer.subscribe();
}
private function start():void {
var msg:AsyncMessage = new AsyncMessage();
msg.body = "start";
producer.send(msg);
consumer.subscribe();
}
private function handleFault(event:MessageFaultEvent):void {
Alert.show(event.faultString);
}
private function ack(event:MessageAckEvent):void {
if (event.message.body != null) {
randomNumbers = event.message.body as String;
Alert.show(randomNumbers.toString());
}
}
<mx:Producer id="producer" destination="RandomDataPush" acknowledge="ack(event)"/>
<mx:Consumer id="consumer" destination="RandomDataPush" message="messageHandler(event.message)" />
</mx:Application>
Problems are:
1) Whitout a thread I can't manage to push messages
2) The thread starts automatically when I alunch the application. To prevent this I send the "stop" when the init() method of the flex page is called (but is really orrible)
What I want to do is: when a java query is executed (or a complex elaboration starts) I want to start the service and send messages calling manually the sendMessageToClients method (without thread if is it possible) and, when the query or the elaboration ends, stop the service.
Can you help me? Thanks all