001/* 002 * (C) Copyright 2017 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * bdelbosc 018 */ 019package org.nuxeo.runtime.pubsub; 020 021import java.time.Duration; 022import java.util.List; 023import java.util.Map; 024import java.util.Random; 025import java.util.function.BiConsumer; 026 027import org.apache.commons.lang3.StringUtils; 028import org.apache.commons.logging.Log; 029import org.apache.commons.logging.LogFactory; 030import org.nuxeo.lib.stream.computation.Record; 031import org.nuxeo.lib.stream.log.LogAppender; 032import org.nuxeo.lib.stream.log.LogRecord; 033import org.nuxeo.lib.stream.log.LogTailer; 034import org.nuxeo.runtime.api.Framework; 035import org.nuxeo.runtime.stream.StreamService; 036 037/** 038 * A Pub/Sub provider based on Nuxeo Stream. 039 * 040 * @since 10.1 041 */ 042public class StreamPubSubProvider extends AbstractPubSubProvider { 043 private static final Log log = LogFactory.getLog(StreamPubSubProvider.class); 044 045 public static final String GROUP_PREFIX = "pub-sub-node-"; 046 047 protected static final String NODE_ID_PROP = "repository.clustering.id"; 048 049 protected static final String LOG_CONFIG_OPT = "logConfig"; 050 051 protected static final String DEFAULT_LOG_CONFIG = "default"; 052 053 protected static final String LOG_NAME_OPT = "logName"; 054 055 protected static final Random RANDOM = new Random(); 056 057 protected String logConfig; 058 059 protected String logName; 060 061 protected LogAppender<Record> appender; 062 063 protected Thread thread; 064 065 @Override 066 public void initialize(Map<String, String> options, Map<String, List<BiConsumer<String, byte[]>>> subscribers) { 067 log.debug("Initializing "); 068 super.initialize(options, subscribers); 069 logConfig = options.getOrDefault(LOG_CONFIG_OPT, DEFAULT_LOG_CONFIG); 070 logName = options.get(LOG_NAME_OPT); 071 if (StringUtils.isBlank(logName)) { 072 throw new IllegalArgumentException("Missing option logName in StreamPubSubProviderDescriptor"); 073 } 074 appender = Framework.getService(StreamService.class).getLogManager(logConfig).getAppender(logName); 075 startConsumerThread(); 076 log.debug("Initialized"); 077 } 078 079 protected void startConsumerThread() { 080 Subscriber subscriber = new Subscriber(); 081 thread = new Thread(subscriber, "Nuxeo-PubSub-Stream"); 082 thread.setUncaughtExceptionHandler((t, e) -> log.error("Uncaught error on thread " + t.getName(), e)); 083 thread.setPriority(Thread.NORM_PRIORITY); 084 thread.setDaemon(true); 085 thread.start(); 086 } 087 088 @Override 089 public void publish(String topic, byte[] message) { 090 appender.append(topic, Record.of(topic, message)); 091 } 092 093 @Override 094 public void close() { 095 appender = null; 096 if (thread != null) { 097 thread.interrupt(); 098 thread = null; 099 log.debug("Closed"); 100 } 101 } 102 103 public class Subscriber implements Runnable { 104 105 @Override 106 public void run() { 107 // using different group name enable fan out 108 String group = GROUP_PREFIX + getNodeId(); 109 log.debug("Starting subscriber thread with group: " + group); 110 try (LogTailer<Record> tailer = Framework.getService(StreamService.class) 111 .getLogManager(logConfig) 112 .createTailer(group, logName)) { 113 // Only interested in new messages 114 tailer.toEnd(); 115 for (;;) { 116 try { 117 LogRecord<Record> logRecord = tailer.read(Duration.ofSeconds(5)); 118 if (logRecord == null) { 119 continue; 120 } 121 Record record = logRecord.message(); 122 localPublish(record.key, record.data); 123 } catch (InterruptedException e) { 124 Thread.currentThread().interrupt(); 125 log.debug("Subscriber thread interrupted, exiting"); 126 return; 127 } 128 } 129 } 130 131 } 132 } 133 134 protected String getNodeId() { 135 String nodeId = Framework.getProperty(NODE_ID_PROP); 136 if (StringUtils.isBlank(nodeId)) { 137 return String.valueOf(RANDOM.nextLong()); 138 } 139 return nodeId.trim(); 140 } 141}