@Override public void publish(Object[] payload) { if (!isEnabled()) { throw new RuntimeException("Statistics publisher is not enabled"); } Event event = new Event(); event.setPayloadData(payload); event.setArbitraryDataMap(new HashMap<String, String>()); try { if (log.isDebugEnabled()) { log.debug( String.format( "Publishing cep event: [stream] %s [version] %s", streamDefinition.getName(), streamDefinition.getVersion())); } asyncDataPublisher.publish(streamDefinition.getName(), streamDefinition.getVersion(), event); } catch (AgentException e) { if (log.isErrorEnabled()) { log.error( String.format( "Could not publish cep event: [stream] %s [version] %s", streamDefinition.getName(), streamDefinition.getVersion()), e); } } }
private void init() { AgentConfiguration agentConfiguration = new AgentConfiguration(); Agent agent = new Agent(agentConfiguration); // Initialize asynchronous data publisher asyncDataPublisher = new AsyncDataPublisher("tcp://" + ip + ":" + port + "", username, password, agent); asyncDataPublisher.addStreamDefinition(streamDefinition); }
/** * @param topicName - topic name to publish messages * @param message - is and Object[]{Event, EventDefinition} * @param brokerConfiguration - broker configuration to be used * @throws BrokerEventProcessingException */ public void publish(String topicName, Object message, BrokerConfiguration brokerConfiguration) throws BrokerEventProcessingException { Integer tenantId = CarbonContext.getCurrentContext().getTenantId(); ConcurrentHashMap<BrokerConfiguration, AsyncDataPublisher> dataPublishers = dataPublisherMap.get(tenantId); if (dataPublishers == null) { dataPublishers = new ConcurrentHashMap<BrokerConfiguration, AsyncDataPublisher>(); dataPublisherMap.putIfAbsent(tenantId, dataPublishers); dataPublishers = dataPublisherMap.get(tenantId); } AsyncDataPublisher dataPublisher = dataPublishers.get(brokerConfiguration); if (dataPublisher == null) { synchronized (this) { dataPublisher = dataPublishers.get(brokerConfiguration); if (dataPublisher == null) { dataPublisher = createDataPublisher(brokerConfiguration); dataPublishers.putIfAbsent(brokerConfiguration, dataPublisher); } } } try { Event event = (Event) ((Object[]) message)[0]; StreamDefinition streamDefinition = (StreamDefinition) ((Object[]) message)[1]; // String streamId = outputStreamIdMap.get(topicName + tenantId); if (!dataPublisher.isStreamDefinitionAdded(streamDefinition)) { dataPublisher.addStreamDefinition(streamDefinition); // Sending the first Event publishEvent(brokerConfiguration, dataPublisher, event, streamDefinition); } else { // Sending Events publishEvent(brokerConfiguration, dataPublisher, event, streamDefinition); } } catch (Exception ex) { throw new BrokerEventProcessingException( ex.getMessage() + " Error Occurred When Publishing Events", ex); } }
private void publishEvent( BrokerConfiguration brokerConfiguration, AsyncDataPublisher dataPublisher, Event event, StreamDefinition streamDefinition) throws BrokerEventProcessingException { try { dataPublisher.publish(streamDefinition.getName(), streamDefinition.getVersion(), event); } catch (AgentException ex) { throw new BrokerEventProcessingException( "Cannot publish data via DataPublisher for the broker configuration:" + brokerConfiguration.getName() + " for the event " + event, ex); } }