Connect Delphi and Free Pascal applications with WildFly 10 Final

Earlier this year, Red Hat released WildFly 10 Final, a free and open source application server for the Java platform. If you need to exchange data between Delphi or Free Pascal code and WildFly 10, you can follow these steps for a quick demonstration of the Habari Client for Artemis library from Habarisoft. This native library uses the platform-independent STOMP wire protocol for asynchronous, bidirectional message exchange with popular message brokers, including the default message broker included in WildFly 10, Apache ActiveMQ Artemis.

Part one: basic setup and test

System requirements

  • WildFly 10 requires Java SE 8 or later

Download

Server configuration

  • run <inst>wildfly-10.0.0.Finalbinadd-user and add the application user guest with password guest and role guest
  • edit <inst>wildfly-10.0.0.Finalstandaloneconfigurationstandalone-full.xml to enable STOMP and add the ExampleTopic destination (see below)
<subsystem xmlns="urn:jboss:domain:messaging-activemq:1.0">
<server name="default">
 ...
  <acceptor name="stomp-acceptor" factory-class="org.apache.activemq.artemis.core.remoting.impl.netty.NettyAcceptorFactory">
    <param name="protocols" value="STOMP"/>
    <param name="port" value="61613"/>
  </acceptor>
  ...
  <jms-topic name="ExampleTopic" entries="java:/jms/topic/ExampleTopic" />
  </server>
</subsystem>

Launch server and clients

  1. start WildFly with <inst>wildfly-10.0.0.Finalbinstandalone -c standalone-full.xml
  2. launch one or more instances of the HabariChat demo application and connect to the server on localhost with user guest and password guest

The Delphi application now can use the ExampleTopic as the ‘chat room’ for messages between all running instances (and, of course, allows also non-Delphi clients to receive and send messages within this chat room topic as well).

guestguest

Part two: from Delphi to Java and back

With server and client up and running, our demonstration uses the built-in message broker in WildFly to allow communication between instances of our Delphi demo application.

Chat bot demo

As an advanced use case, we would like to see messages exchanged between the HabariChat demo and a Java program which runs inside the WildFly application server. More specifically, we want to implement two functions:

  • the server receives and logs all messages sent to the ExampleTopic destination
  • the server replies to specific messages

This can be implemented with some lines of code for a Java EE  Message-Driven Bean. The Java source code code for the ChatMessageBean class is shown below.

Neue Bitmap

 

package com.habarisoft.wf10mdb;

import ...;

@MessageDriven(...)
@ApplicationScoped
public class ChatMessageBean implements MessageListener {

    @Override
    public void onMessage(Message message) {
        try {

            final TextMessage textMessage = (TextMessage) message;
            final String question = textMessage.getText();

            System.out.println(question);

            if (null != question) {
                switch (question) {
                    case &quot;Hello World!&quot;:
                        respond(&quot;Hello, Test Case!&quot;);
                        break;
                    case &quot;How are you?&quot;:
                        respond(&quot;I'm doing well.&quot;);
                        break;
                    case &quot;Still spinning?&quot;:
                        respond(&quot;Once every day, as usual.&quot;);
                        break;
                }
            }
        } catch (JMSException e) {
            throw new IllegalStateException(e);
        }
    }

    @Resource(mappedName = &quot;java:/jms/topic/ExampleTopic&quot;)
    private Topic answerTopic;

    @Inject
    private JMSContext context;

    public void respond(String txt) {
        try {
            context.createProducer().send(answerTopic, txt);
        } catch (Exception exc) {
            exc.printStackTrace(System.out);
        }
    }
}

Related posts

 

Advertisements