org.wso2.carbon.databridge.commons.Event Java Examples

The following examples show how to use org.wso2.carbon.databridge.commons.Event. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DataEndpointGroup.java    From product-microgateway with Apache License 2.0 6 votes vote down vote up
@Override
public void onEvent(WrappedEventFactory.WrappedEvent wrappedEvent, long sequence, boolean endOfBatch) {
    DataEndpoint endpoint = getDataEndpoint(true);
    Event event = wrappedEvent.getEvent();
    if (endpoint != null) {
        isLastEventDropped = false;
        endpoint.collectAndSend(event);
        if (endOfBatch) {
            flushAllDataEndpoints();
        }
    } else {
        if (!isLastEventDropped) {
            log.error("Dropping all events as DataPublisher is shutting down.");
        }
        if (log.isDebugEnabled()) {
            log.debug("Data publisher is shutting down, dropping event : " + event);
        }
        isLastEventDropped = true;
    }
}
 
Example #2
Source File: Client.java    From product-cep with Apache License 2.0 6 votes vote down vote up
private static void sendWarmUpEvents(DataPublisher dataPublisher, long warmUpCount) {
    long counter = 0;
    Random randomGenerator = new Random();
    String streamId = "org.wso2.event.sensor.stream:1.0.0";

    while (counter < warmUpCount) {
        boolean isPowerSaveEnabled = randomGenerator.nextBoolean();
        int sensorId = randomGenerator.nextInt();
        double longitude = randomGenerator.nextDouble();
        double latitude = randomGenerator.nextDouble();
        float humidity = randomGenerator.nextFloat();
        double sensorValue = randomGenerator.nextDouble();
        Event event = new Event(streamId, System.currentTimeMillis(),
                new Object[]{System.currentTimeMillis(), isPowerSaveEnabled, sensorId,
                        "warmup-" + counter},
                new Object[]{longitude, latitude},
                new Object[]{humidity, sensorValue});

        dataPublisher.publish(event);
        counter++;
    }
}
 
Example #3
Source File: DataEndpointGroup.java    From product-microgateway with Apache License 2.0 6 votes vote down vote up
private void put(Event event) {
    do {
        try {
            long sequence = this.ringBuffer.tryNext(1);
            WrappedEventFactory.WrappedEvent bufferedEvent = this.ringBuffer.get(sequence);
            bufferedEvent.setEvent(event);
            this.ringBuffer.publish(sequence);
            return;
        } catch (InsufficientCapacityException ex) {
            try {
                Thread.sleep(2);
            } catch (InterruptedException ignored) {
            }
        }
    } while (isActiveDataEndpointExists());
}
 
Example #4
Source File: DataPublisher.java    From attic-stratos with Apache License 2.0 6 votes vote down vote up
public void publish(DataContext dataContext) {

        Event event = new Event();
        event.setTimeStamp(new Date().getTime());
        event.setMetaData(dataContext.getMetaData());
        event.setPayloadData(dataContext.getPayloadData());

        try {
            dataPublisher.publish(streamDefinition.getName(), streamDefinition.getVersion(), event);

        } catch (AgentException e) {
            String errorMsg = "Error in publishing event";
            log.error(errorMsg, e);
            // no need to throw here
        }
    }
 
Example #5
Source File: HttpdLogAgent.java    From product-cep with Apache License 2.0 6 votes vote down vote up
private static void publishLogEvents(DataPublisher dataPublisher, String streamId) throws FileNotFoundException {
    Scanner scanner = new Scanner(new FileInputStream(SAMPLE_LOG_PATH));
    try {
        int i = 1;
        while (scanner.hasNextLine()) {
            System.out.println("Publish log event : " + i);
            String aLog = scanner.nextLine();
            Event event = new Event(streamId, System.currentTimeMillis(), new Object[]{"external"}, null,
                                    new Object[]{aLog});
            dataPublisher.publish(event);
            i++;
        }
    } finally {
        scanner.close();
    }
}
 
Example #6
Source File: HTTPMonitoringDataPublisher.java    From msf4j with Apache License 2.0 6 votes vote down vote up
public void publishEvent(HTTPMonitoringEvent httpMonitoringEvent) {
    Object[] meta = new Object[4];
    meta[0] = httpMonitoringEvent.getTimestamp();
    meta[1] = SERVER_HOST_ADDRESS;
    meta[2] = SERVER_HOSTNAME;
    meta[3] = MICROSERVICE;
    Object[] correlation = new Object[2];
    correlation[0] = httpMonitoringEvent.getActivityId();
    correlation[1] = httpMonitoringEvent.getParentRequest();
    Object[] payload = new Object[11];
    payload[0] = httpMonitoringEvent.getServiceClass();
    payload[1] = httpMonitoringEvent.getServiceName();
    payload[2] = httpMonitoringEvent.getServiceMethod();
    payload[3] = httpMonitoringEvent.getRequestUri();
    payload[4] = httpMonitoringEvent.getServiceContext();
    payload[5] = httpMonitoringEvent.getHttpMethod();
    payload[6] = httpMonitoringEvent.getContentType();
    payload[7] = httpMonitoringEvent.getRequestSizeBytes();
    payload[8] = httpMonitoringEvent.getReferrer();
    payload[9] = httpMonitoringEvent.getResponseHttpStatusCode();
    payload[10] = httpMonitoringEvent.getResponseTime();

    Event event = new Event(HTTP_MONITORING_STREAM_ID, httpMonitoringEvent.getTimestamp(), meta, correlation,
            payload, arbitraryAttributes);
    dataPublisher.publish(event);
}
 
Example #7
Source File: APIAuthenticationAdminClient.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
public void invalidateResourceCache(String apiContext, String apiVersion, Set<URITemplate> uriTemplates) {

        JSONObject api = new JSONObject();
        api.put("apiContext", apiContext);
        api.put("apiVersion", apiVersion);
        JSONArray resources = new JSONArray();
        for (URITemplate uriTemplate : uriTemplates) {
            JSONObject resource = new JSONObject();
            resource.put("resourceURLContext", uriTemplate.getUriTemplate());
            resource.put("httpVerb", uriTemplate.getHTTPVerb());
            resources.add(resource);
        }
        api.put("resources", resources);

        Object[] objectData = new Object[]{APIConstants.RESOURCE_CACHE_NAME, api.toJSONString()};
        log.debug("Sending Resource Invalidation Message");
        Event event = new Event(APIConstants.CACHE_INVALIDATION_STREAM_ID, System.currentTimeMillis(),
                null, null, objectData);
        APIUtil.publishEventToEventHub(null, event);
    }
 
Example #8
Source File: KeyMgtNotificationSender.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
public void notify(KeyManagerConfigurationDTO keyManagerConfigurationDTO,String action) {
    String encodedString = "";
    if (keyManagerConfigurationDTO.getAdditionalProperties() != null){
        String additionalProperties = new Gson().toJson(keyManagerConfigurationDTO.getAdditionalProperties());
        encodedString = new String(Base64.encodeBase64(additionalProperties.getBytes()));
    }
    Object[] objects = new Object[]{APIConstants.KeyManager.KeyManagerEvent.KEY_MANAGER_CONFIGURATION, action,
            keyManagerConfigurationDTO.getName(), keyManagerConfigurationDTO.getType(),
            keyManagerConfigurationDTO.isEnabled(), encodedString,
            keyManagerConfigurationDTO.getTenantDomain()};
    Event keyManagerEvent = new Event(APIConstants.KeyManager.KeyManagerEvent.KEY_MANAGER_STREAM_ID,
            System.currentTimeMillis(),
            null, null, objects);
    EventHubConfigurationDto eventHubConfigurationDto =
            ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration()
                    .getEventHubConfigurationDto();

    if (eventHubConfigurationDto.isEnabled()) {
        APIUtil.publishEventToTrafficManager(Collections.EMPTY_MAP, keyManagerEvent);
    }
}
 
Example #9
Source File: BinaryDataEndpoint.java    From product-microgateway with Apache License 2.0 6 votes vote down vote up
@Override
protected void send(Object client, List<Event> events) throws DataEndpointException,
        SessionTimeoutException, UndefinedEventTypeException {
    Socket socket = (Socket) client;
    String sessionId = getDataEndpointConfiguration().getSessionId();
    try {
        sendBinaryPublishMessage(socket, events, sessionId);
        processResponse(socket);
    } catch (Exception e) {
        if (e instanceof DataEndpointException) {
            throw (DataEndpointException) e;
        } else if (e instanceof UndefinedEventTypeException) {
            throw new UndefinedEventTypeException("Undefined Event Type Exception ", e);
        } else if (e instanceof SessionTimeoutException) {
            throw new SessionTimeoutException("Binary Session Expired Exception ", e);
        } else {
            throw new DataEndpointException("Error while trying to publish events to data receiver :"
                    + socket.getRemoteSocketAddress().toString(), e);
        }
    }
}
 
Example #10
Source File: DataEndpoint.java    From product-microgateway with Apache License 2.0 5 votes vote down vote up
void collectAndSend(Event event) {
    events.add(event);
    if (events.size() >= batchSize) {
        threadPoolExecutor.submitJobAndSetState(new EventPublisher(events), this);
        events = new ArrayList<>();
    }
}
 
Example #11
Source File: APIAuthenticationAdminClient.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * Removes given usernames that is cached on the API Gateway
 *
 * @param username_list - The list of usernames to be removed from the gateway cache.
 */
public void invalidateCachedUsernames(String[] username_list) {

    JSONArray userArray = new JSONArray();
    userArray.addAll(Arrays.asList(username_list));
    Object[] objectData = new Object[]{APIConstants.GATEWAY_USERNAME_CACHE_NAME, userArray.toJSONString()};
    Event event = new Event(APIConstants.CACHE_INVALIDATION_STREAM_ID, System.currentTimeMillis(),
            null, null, objectData);
    APIUtil.publishEventToEventHub(null, event);
}
 
Example #12
Source File: DataEndpointGroup.java    From product-microgateway with Apache License 2.0 5 votes vote down vote up
public void tryResendEvents(List<Event> events, DataEndpoint dataEndpoint) {
    List<Event> unsuccessfulEvents = trySendActiveEndpoints(events, dataEndpoint);
    for (Event event : unsuccessfulEvents) {
        try {
            if (eventQueue != null) {
                eventQueue.tryPut(event);
            } else {
                trySyncPublish(event);
            }
        } catch (EventQueueFullException e) {
            log.error("Unable to put the event :" + event, e);
        }
    }
}
 
Example #13
Source File: ConfigurationPublisher.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
private static void publishToAgent(Object[] eventData, Object[] metaData) {
    // Creating Event
    Event event = new Event(streamId, System.currentTimeMillis(), metaData, null, eventData);

    // Has to use try-publish for asynchronous publishing
    DataBridgePublisher.getDataPublisher().publish(event);
}
 
Example #14
Source File: ThriftStatisticsPublisher.java    From attic-stratos with Apache License 2.0 5 votes vote down vote up
@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 thrift event: [stream] %s [version] %s", streamDefinition.getName(),
                    streamDefinition.getVersion()));
        }
        loadBalancingDataPublisher.publish(streamDefinition.getName(), streamDefinition.getVersion(), event);
        if (log.isDebugEnabled()) {
            log.debug(String.format("Successfully Published thrift event: [stream] %s [version] %s",
                    streamDefinition.getName(), streamDefinition.getVersion()));
        }

    } catch (AgentException e) {
        if (log.isErrorEnabled()) {
            log.error(String.format("Could not publish thrift event: [stream] %s [version] %s",
                    streamDefinition.getName(), streamDefinition.getVersion()), e);
        }
    }
}
 
Example #15
Source File: RecommenderDetailsExtractor.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
public void publishEvent(String payload) {

        Object[] objects = new Object[]{payload};
        Event event = new Event(streamID, System.currentTimeMillis(), null, null, objects);
        APIUtil.publishEvent(APIConstants.RECOMMENDATIONS_WSO2_EVENT_PUBLISHER, Collections.EMPTY_MAP, event);
        if (log.isDebugEnabled()) {
            log.debug("Event Published for recommendation server with payload " + payload);
        }
    }
 
Example #16
Source File: DataEndpointGroup.java    From product-microgateway with Apache License 2.0 5 votes vote down vote up
private List<Event> trySendActiveEndpoints(List<Event> events, DataEndpoint failedEP) {
    ArrayList<Event> unsuccessfulEvents = new ArrayList<>();
    for (Event event : events) {
        DataEndpoint endpoint = getDataEndpoint(false, failedEP);
        if (endpoint != null) {
            endpoint.collectAndSend(event);
        } else {
            unsuccessfulEvents.add(event);
        }
    }
    flushAllDataEndpoints();
    return unsuccessfulEvents;
}
 
Example #17
Source File: TokenRevocationNotifierImpl.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * Method to publish the revoked token on to the realtime message broker
 *
 * @param revokedToken requested revoked token
 * @param properties realtime notifier properties read from the config
 */
@Override
public void sendMessageOnRealtime(String revokedToken, Properties properties) {
    //Variables related to Realtime Notifier
    String realtimeNotifierTTL = properties.getProperty("ttl", DEFAULT_TTL);
    long expiryTimeForJWT = Long.parseLong(properties.getProperty("expiryTime"));
    Object[] objects = new Object[]{revokedToken, realtimeNotifierTTL, expiryTimeForJWT};
    Event tokenRevocationMessage = new Event(APIConstants.TOKEN_REVOCATION_STREAM_ID, System.currentTimeMillis(),
            null, null, objects);
    APIUtil.publishEventToEventHub(Collections.EMPTY_MAP, tokenRevocationMessage);
    log.debug("Successfully sent the revoked token notification on realtime");
}
 
Example #18
Source File: DataPublisher.java    From product-microgateway with Apache License 2.0 5 votes vote down vote up
private void onEventQueueFull(DataEndpointGroup endpointGroup, Event event) {
    this.failedEventCount++;
    long currentTime = System.currentTimeMillis();
    if (currentTime - this.lastFailedEventTime > FAILED_EVENT_LOG_INTERVAL) {
        log.warn("Event queue is full, unable to process the event for endpoint group "
                + endpointGroup.toString() + ", " + this.failedEventCount + " events dropped so far.");
        this.lastFailedEventTime = currentTime;
    }
    if (log.isDebugEnabled()) {
        log.debug("Dropped Event: " + event.toString() + " for the endpoint group " +
                endpointGroup.toString());
    }
}
 
Example #19
Source File: DataPublisher.java    From product-microgateway with Apache License 2.0 5 votes vote down vote up
/**
 * Publish an event based on the event properties that are passed
 * for all receiver groups which has been specified in the DataPublisher.
 * This is a non-blocking invocation and if the queue if full
 * then it will simply drop the event.
 *
 * @param event The event which needs to be published to the receiver groups
 * @return the success/failure of the event that has been published/dropped.
 */
public boolean tryPublish(Event event) {
    boolean sent = true;
    for (DataEndpointGroup endpointGroup : endpointGroups) {
        try {
            endpointGroup.tryPublish(event);
            sent = true;
        } catch (EventQueueFullException e) {
            this.onEventQueueFull(endpointGroup, event);
            sent = false;
        }
    }
    return sent;
}
 
Example #20
Source File: StatisticsPublisher.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
private static void publishToAgent(Object[] eventData, Object[] metaData) {
    // Creating Event
    Event event = new Event(streamId, System.currentTimeMillis(), metaData, null, eventData);

    // Has to use try-publish for asynchronous publishing
    DataBridgePublisher.getDataPublisher().publish(event);
}
 
Example #21
Source File: DataEndpoint.java    From product-microgateway with Apache License 2.0 5 votes vote down vote up
void syncSend(Event event) {
    List<Event> events = new ArrayList<>(1);
    events.add(event);
    EventPublisher eventPublisher = new EventPublisher(events);
    setStateBusy();
    acquireImmediateDispatchSemaphore();
    try {
        eventPublisher.run();
    } finally {
        releaseImmediateDispatchSemaphore();
    }
}
 
Example #22
Source File: APIAuthenticationAdminClient.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * Removes the active tokens that are cached on the API Gateway
 * @param activeTokens - The active access tokens to be removed from the gateway cache.
 */
public void invalidateCachedTokens(Set<String> activeTokens) {

    JSONArray tokenArray = new JSONArray();
    tokenArray.addAll(activeTokens);
    Object[] objectData = new Object[]{APIConstants.GATEWAY_KEY_CACHE_NAME, tokenArray.toJSONString()};
    Event event = new Event(APIConstants.CACHE_INVALIDATION_STREAM_ID, System.currentTimeMillis(),
            null, null, objectData);
    APIUtil.publishEventToEventHub(null, event);
}
 
Example #23
Source File: DataProcessAndPublishingAgentTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Test
public void setDataReference() throws Exception {
    ThrottleProperties throttleProperties = new ThrottleProperties();
    DataPublisher dataPublisher = Mockito.mock(DataPublisher.class);
    Mockito.when(dataPublisher.tryPublish(Mockito.any(Event.class))).thenReturn(true);
    throttleProperties.setEnabled(true);
    DataProcessAndPublishingAgent dataProcessAndPublishingAgent = new DataProcessAndPublishingAgentWrapper
            (throttleProperties);
    AuthenticationContext authenticationContext = new AuthenticationContext();
    MessageContext messageContext = Mockito.mock(Axis2MessageContext.class);
    org.apache.axis2.context.MessageContext axis2MsgCntxt = Mockito.mock(org.apache.axis2.context.MessageContext
            .class);
    Mockito.when(((Axis2MessageContext) messageContext).getAxis2MessageContext()).thenReturn(axis2MsgCntxt);
    Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API)).thenReturn("admin--PizzaShackAPI");
    VerbInfoDTO verbInfoDTO = new VerbInfoDTO();
    verbInfoDTO.setContentAware(false);
    ArrayList<VerbInfoDTO> list = new ArrayList<VerbInfoDTO>();
    list.add(verbInfoDTO);
    Mockito.when(messageContext.getProperty(APIConstants.VERB_INFO_DTO)).thenReturn(list);
    Mockito.when(axis2MsgCntxt.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS))
            .thenReturn(new TreeMap<>());
    Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API)).thenReturn("admin--PizzaShackAPI");
    dataProcessAndPublishingAgent.setDataReference(applicationLevelThrottleKey, applicationLevelTier,
            apiLevelThrottleKey, apiLevelTier, subscriptionLevelThrottleKey, subscriptionLevelTier,
            resourceLevelThrottleKey, resourceLevelTier, authorizedUser, apiContext, apiVersion, appTenant,
            apiTenant, appId, messageContext, authenticationContext);
    dataProcessAndPublishingAgent.run();
}
 
Example #24
Source File: FileDataPublisher.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Override
public void onEvent(WrappedEventFactory.WrappedEvent wrappedEvent, long sequence, boolean endOfBatch) {
    Event event = wrappedEvent.getEvent();
    StringBuilder builder = new StringBuilder();
    builder.append(MicroGatewayAPIUsageConstants.STREAM_ID)
            .append(MicroGatewayAPIUsageConstants.KEY_VALUE_SEPARATOR)
            .append(event.getStreamId())
            .append(MicroGatewayAPIUsageConstants.EVENT_SEPARATOR);
    builder.append(MicroGatewayAPIUsageConstants.TIME_STAMP)
            .append(MicroGatewayAPIUsageConstants.KEY_VALUE_SEPARATOR)
            .append(event.getTimeStamp())
            .append(MicroGatewayAPIUsageConstants.EVENT_SEPARATOR);
    builder.append(MicroGatewayAPIUsageConstants.META_DATA)
            .append(MicroGatewayAPIUsageConstants.KEY_VALUE_SEPARATOR)
            .append((event.getMetaData() == null ? null :
                    StringUtils.join(event.getMetaData(), MicroGatewayAPIUsageConstants.OBJECT_SEPARATOR)))
            .append(MicroGatewayAPIUsageConstants.EVENT_SEPARATOR);
    builder.append(MicroGatewayAPIUsageConstants.CORRELATION_DATA)
            .append(MicroGatewayAPIUsageConstants.KEY_VALUE_SEPARATOR)
            .append((event.getCorrelationData() == null ? null :
                    StringUtils.join(event.getCorrelationData(), MicroGatewayAPIUsageConstants.OBJECT_SEPARATOR)))
            .append(MicroGatewayAPIUsageConstants.EVENT_SEPARATOR);
    builder.append(MicroGatewayAPIUsageConstants.PAYLOAD_DATA)
            .append(MicroGatewayAPIUsageConstants.KEY_VALUE_SEPARATOR)
            .append((event.getPayloadData() == null ? null :
                    StringUtils.join(event.getPayloadData(), MicroGatewayAPIUsageConstants.OBJECT_SEPARATOR)));
    try {
        UsageFileWriter.getInstance().writeToFile(builder.toString());
    } catch (UsagePublisherException e) {
        log.warn("Error occurred while getting the Usage File Writer.", e);
    }
}
 
Example #25
Source File: FileDataPublisher.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
private void tryPut(Event event) throws EventQueueFullException {
    long sequence;
    try {
        sequence = this.ringBuffer.tryNext(1);
        WrappedEventFactory.WrappedEvent bufferedEvent = this.ringBuffer.get(sequence);
        bufferedEvent.setEvent(event);
        this.ringBuffer.publish(sequence);
    } catch (InsufficientCapacityException e) {
        throw new EventQueueFullException("Cannot persist events because the event queue is full", e);
    }
}
 
Example #26
Source File: FileDataPublisher.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
private void onEventQueueFull(Event event) {
    this.failedEventCount++;
    long currentTime = System.currentTimeMillis();
    if (currentTime - this.lastFailedEventTime > FAILED_EVENT_LOG_INTERVAL) {
        log.warn("Event queue is full, unable to process the event, " + this.failedEventCount
                + " events dropped so far.");
        this.lastFailedEventTime = currentTime;
    }
    if (log.isDebugEnabled()) {
        log.debug("Dropped Event: " + event.toString());
    }
}
 
Example #27
Source File: FileDataPublisher.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
private boolean tryPublish(Event event) {
    boolean sent = true;
    try {
        if (eventQueue != null) {
            eventQueue.tryPut(event);
        }
    } catch (EventQueueFullException e) {
        this.onEventQueueFull(event);
        sent = false;
    }
    return sent;
}
 
Example #28
Source File: Client.java    From product-cep with Apache License 2.0 5 votes vote down vote up
private static void publishEventsForLatency(DataPublisher dataPublisher, long eventCount, long elapsedCount,
                                            long warmUpCount) {
    sendWarmUpEvents(dataPublisher, warmUpCount);
    long counter = 0;
    Random randomGenerator = new Random();
    String streamId = "org.wso2.event.sensor.stream:1.0.0";

    while (counter < eventCount) {
        boolean isPowerSaveEnabled = randomGenerator.nextBoolean();
        int sensorId = randomGenerator.nextInt();
        double longitude = randomGenerator.nextDouble();
        double latitude = randomGenerator.nextDouble();
        float humidity = randomGenerator.nextFloat();
        double sensorValue = randomGenerator.nextDouble();
        Event event = new Event(streamId, System.currentTimeMillis(),
                new Object[]{System.currentTimeMillis(), isPowerSaveEnabled, sensorId,
                        "temperature-" + counter},
                new Object[]{longitude, latitude},
                new Object[]{humidity, sensorValue});

        dataPublisher.publish(event);
        log.info("Sent event " + counter + " at " + System.currentTimeMillis());

        if (elapsedCount > 0) {
            try {
                Thread.sleep(elapsedCount);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        counter++;
    }
}
 
Example #29
Source File: Client.java    From product-cep with Apache License 2.0 5 votes vote down vote up
private static void publishEvents(DataPublisher dataPublisher, long eventCount, long elapsedCount,
                                  long warmUpCount) {
    long counter = 0;
    Random randomGenerator = new Random();
    String streamId = "org.wso2.event.sensor.stream:1.0.0";
    long lastTime = System.currentTimeMillis();
    DecimalFormat decimalFormat = new DecimalFormat("#");

    while (counter < eventCount) {
        boolean isPowerSaveEnabled = randomGenerator.nextBoolean();
        int sensorId = randomGenerator.nextInt();
        double longitude = randomGenerator.nextDouble();
        double latitude = randomGenerator.nextDouble();
        float humidity = randomGenerator.nextFloat();
        double sensorValue = randomGenerator.nextDouble();
        Event event = new Event(streamId, System.currentTimeMillis(),
                new Object[]{System.currentTimeMillis(), isPowerSaveEnabled, sensorId,
                        "temperature-" + counter},
                new Object[]{longitude, latitude},
                new Object[]{humidity, sensorValue});

        dataPublisher.publish(event);

        if ((counter > warmUpCount) && ((counter + 1) % elapsedCount == 0)) {

            long currentTime = System.currentTimeMillis();
            long elapsedTime = currentTime - lastTime;
            double throughputPerSecond = (((double) elapsedCount) / elapsedTime) * 1000;
            lastTime = currentTime;
            log.info("Sent " + elapsedCount + " sensor events in " + elapsedTime
                    + " milliseconds with total throughput of " + decimalFormat.format(throughputPerSecond)
                    + " events per second.");
        }

        counter++;
    }
}
 
Example #30
Source File: TestWso2EventServer.java    From product-cep with Apache License 2.0 5 votes vote down vote up
@Override
public void receive(List<Event> eventList, Credentials credentials) {
    long currentTime = System.currentTimeMillis();
    long currentBatchTotalDelay = 0;
    for (Event event : eventList) {
        currentBatchTotalDelay = currentBatchTotalDelay + (currentTime - event.getTimeStamp());
    }
    /** Following section should ideally be atomic **/
    long localTotalDelay = totalDelay.addAndGet(currentBatchTotalDelay);
    long localCounter = counter.addAndGet(eventList.size());
    /** End of wish for atomic section **/

    long index = localCounter / elapsedCount;

    if (lastIndex.get() != index) {
        if (calcInProgress.compareAndSet(false, true)) {
            //TODO Can be made thread safe further
            lastIndex.set(index);
            long currentWindowEventsReceived = localCounter - lastCounter.getAndSet(localCounter);
            //log.info("Current time: " + System.currentTimeMillis() + ", Event received time: " + currentTime + ", Last calculation time: " + lastTime.get());
            long elapsedTime = currentTime - lastTime.getAndSet(currentTime);
            double throughputPerSecond = (((double) currentWindowEventsReceived) / elapsedTime) * 1000;

            log.info("[" + Thread.currentThread().getName() + "] Received " + currentWindowEventsReceived + " sensor events in " + elapsedTime
                    + " milliseconds with total throughput of " + decimalFormat.format(throughputPerSecond)
                    + " events per second. Average delay is " + decimalFormat.format(localTotalDelay / (double) currentWindowEventsReceived));
            totalDelay.addAndGet(-localTotalDelay);
            calcInProgress.set(false);
        }
    }
}