Highlighted

Send message to particular client via messaging

endryha

31-07-2009

I want to send message to particular user via messaging (consumer/producer).

I have following code for this:

**********************************************************************

public void sendMessageToClient(Object msgBody, String clientId) {
        MessageBroker.getMessageBroker(null).routeMessageToMessageClient(createMessage(msgBody), getMessageClient(clientId));
    }

    private MessageClient getMessageClient(String clientId) {
        MessageBroker messageBroker = MessageBroker.getMessageBroker(null);
        Object serviceId = messageBroker.getDestinationService().get("informer");
        MessageService messageService = (MessageService) messageBroker.getServices().get(serviceId);
        MessageDestination messageDestination = (MessageDestination) messageService.getDestination("informer");
        MessageClient messageClient = messageDestination.getSubscriptionManager().getSubscriber(clientId);
        return messageClient;
    }

    private AsyncMessage createMessage(Object msgBody) {
        AsyncMessage msg = new AsyncMessage();
        // msg.setClientId(UUIDUtils.createUUID(true));
        msg.setTimestamp(System.currentTimeMillis());
        msg.setMessageId(UUIDUtils.createUUID(true));
        msg.setDestination("informer");
        msg.setBody(msgBody);
        // msg.setHeader("sender", "From the server");
        return msg;
    }

**********************************************************************

and corresponding configurations in messaging-config.xml :

**********************************************************************

<destination id="informer">
        <properties>
            <network>
                <session-timeout>0</session-timeout>
            </network>
            <server>
                <max-cache-size>1000</max-cache-size>
                <message-time-to-live>0</message-time-to-live>
                <durable>false</durable>
            </server>
        </properties>
        <channels>
            <channel ref="channel-rtmp" />
            <channel ref="channel-amf" />
            <channel ref="channel-polling-amf" />
        </channels>
    </destination>

**********************************************************************

but it throws NullPointerException at flex.messaging.MessageBroker.routeMessageToMessageClient(MessageBroker.java:1372)

any suggestions?

Replies

Highlighted

endryha

31-07-2009

I has understood wath's the reason of this exception. It's because i am tryin to send message to client via lcds messaging (consumer/producer) with simple flex client id and in practice ids for consumer is different and i am don't know where i can get this id. I can see it by messageDestination.getSubscriptionManager().getSubscriberIds() code, but i can't get consumer client id of current user. Can anybody help me?

Highlighted

endryha

04-08-2009

I have resolved this issue. To send message to client consumer you can use following code

<code>

    public void sendMessageToClient(Object msgBody, Object clientId) {
        Set clientIds = new TreeSet();
        clientIds.add(clientId);
        sendMessageToClients(msgBody, clientIds);
    }

    public void sendMessageToClients(Object msgBody, Set clientIds) {
        log.debug("send message to clients [ids = " + clientIds + ", message = " + msgBody + "]");
        MessageBroker messageBroker = MessageBroker.getMessageBroker(null);
        Object serviceId = messageBroker.getDestinationService().get(MESSAGING_SERVICE_NAME);
        MessageService messageService = (MessageService) messageBroker.getServices().get(serviceId);
        MessageDestination messageDestination = (MessageDestination) messageService.getDestination(MESSAGING_SERVICE_NAME);
        messageService.pushMessageToClients(clientIds, createMessage(msgBody), false);
    }

    private AsyncMessage createMessage(Object msgBody) {
        AsyncMessage msg = new AsyncMessage();
        msg.setClientId(SERVER_ID);
        msg.setTimestamp(System.currentTimeMillis());
        msg.setMessageId(UUIDUtils.createUUID(true));
        msg.setDestination(MESSAGING_SERVICE_NAME);
        msg.setBody(msgBody);
        return msg;
    }

</code>

where client id is consumer client id. If somebody have a questions email me endryha@gmail.com