Search in sources :

Example 1 with MessageSubscriber

use of org.structr.messaging.engine.entities.MessageSubscriber in project structr by structr.

the class MQTTClient method getTopics.

static String[] getTopics(MQTTClient thisClient) {
    final App app = StructrApp.getInstance();
    try (final Tx tx = app.tx()) {
        List<MessageSubscriber> subs = thisClient.getSubscribers();
        String[] topics = new String[subs.size()];
        for (int i = 0; i < subs.size(); i++) {
            topics[i] = subs.get(i).getTopic();
        }
        return topics;
    } catch (FrameworkException ex) {
        logger.error("Couldn't retrieve client topics for MQTT subscription.");
        return null;
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) MessageSubscriber(org.structr.messaging.engine.entities.MessageSubscriber) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException)

Example 2 with MessageSubscriber

use of org.structr.messaging.engine.entities.MessageSubscriber in project structr by structr.

the class KafkaClient method refreshSubscriptions.

static void refreshSubscriptions(KafkaClient thisClient) {
    final App app = StructrApp.getInstance();
    try (final Tx tx = app.tx()) {
        thisClient.getSubscribers().forEach((MessageSubscriber sub) -> {
            try {
                String topic = sub.getProperty(StructrApp.key(MessageSubscriber.class, "topic"));
                if (topic != null) {
                    subscribeTopic(thisClient, topic);
                }
            } catch (FrameworkException e) {
                logger.warn("Could not subscribe to topic in KafkaClient: " + e.getMessage());
            }
        });
        tx.success();
    } catch (FrameworkException ex) {
        logger.error("Could not refresh subscriptions in KafkaClient: " + ex.getMessage());
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) MessageSubscriber(org.structr.messaging.engine.entities.MessageSubscriber) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException)

Example 3 with MessageSubscriber

use of org.structr.messaging.engine.entities.MessageSubscriber in project structr by structr.

the class SimpleMessagingTest method test01.

@Test
public void test01() {
    try (final Tx tx = app.tx()) {
        MessageClient client1 = app.create(MessageClient.class, "client1");
        MessageSubscriber sub = app.create(MessageSubscriber.class, "sub");
        List<MessageSubscriber> subList = new ArrayList<>();
        subList.add(sub);
        client1.setProperty(StructrApp.key(MessageClient.class, "subscribers"), subList);
        sub.setProperty(StructrApp.key(MessageSubscriber.class, "topic"), "test");
        sub.setProperty(StructrApp.key(MessageSubscriber.class, "callback"), "set(this, 'name', retrieve('message'))");
        Scripting.replaceVariables(new ActionContext(securityContext, null), client1, "${{Structr.log('Sending message'); Structr.get('this').sendMessage('test','testmessage');}}");
        assertEquals("testmessage", sub.getName());
        tx.success();
    } catch (FrameworkException ex) {
        fail("Exception during test: " + ex.getMessage());
    }
}
Also used : MessageSubscriber(org.structr.messaging.engine.entities.MessageSubscriber) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) ArrayList(java.util.ArrayList) ActionContext(org.structr.schema.action.ActionContext) MessageClient(org.structr.messaging.engine.entities.MessageClient) Test(org.junit.Test)

Aggregations

FrameworkException (org.structr.common.error.FrameworkException)3 Tx (org.structr.core.graph.Tx)3 MessageSubscriber (org.structr.messaging.engine.entities.MessageSubscriber)3 App (org.structr.core.app.App)2 StructrApp (org.structr.core.app.StructrApp)2 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 MessageClient (org.structr.messaging.engine.entities.MessageClient)1 ActionContext (org.structr.schema.action.ActionContext)1