DDE Advice

Приведенный ниже пример подписывается на события от DDE сервера. В качестве DDE сервера можно использовать наш пример.

final String SERVICE = "MyServer";
final String TOPIC = "MyTopic";
final String ITEM = "MyAdvise";

try
{
    // event to wait disconnection
    final CountDownLatch eventDisconnect = new CountDownLatch(1);

    // DDE client
    final DDEClientConversation conversation = new DDEClientConversation();
    // We can use UNICODE format if server prefers it
    //conversation.setTextFormat(ClipboardFormat.CF_UNICODETEXT);

    conversation.setEventListener(new DDEClientEventListener()
    {
        public void onDisconnect()
        {
            System.out.println("onDisconnect()");
            eventDisconnect.countDown();
        }

        public void onItemChanged(String topic, String item, String data)
        {
            System.out.println("onItemChanged(" + topic + "," + item + "," + data + ")");
            try
            {
                if ("stop".equalsIgnoreCase(data))
                    conversation.stopAdvice(item);
            }
            catch (DDEException e)
            {
                System.out.println("Exception: " + e);
            }
        }
    });

    System.out.println("Connecting...");
    conversation.connect(SERVICE, TOPIC);
    conversation.startAdvice(ITEM);

    System.out.println("Waiting event...");
    eventDisconnect.await();
    System.out.println("Disconnecting...");
    conversation.disconnect();
    System.out.println("Exit from thread");
}
catch (DDEMLException e)
{
    System.out.println("DDEMLException: 0x" + Integer.toHexString(e.getErrorCode()) + " " + e.getMessage());
}
catch (DDEException e)
{
    System.out.println("DDEClientException: " + e.getMessage());
}
catch (Exception e)
{
    System.out.println("Exception: " + e);
}

Скачать AdviceExample.java
Скачать все примеры и библиотеку

Запуск примера из консоли:

java -cp .;pretty-tools-JDDE-2.1.0.jar AdviceExample