Package com.betfair.cougar.core.api.ev

Examples of com.betfair.cougar.core.api.ev.ExecutionResult


        final NewHeapSubscription newHeapSubscription;
        try {
            newHeapSubscription = (NewHeapSubscription) in.getResult();
        } catch (Exception e) {
            logger.log(Level.WARNING, "Error unpacking subscription result", e);
            observer.onResult(new ExecutionResult(new CougarServiceException(ServerFaultCode.FrameworkError, "Error unpacking subscription result", e)));
            return;
        }
        nioLogger.log(NioLogger.LoggingLevel.TRANSPORT, currentSession, "Received a subscription response for heapId %s with subscriptionId %s", newHeapSubscription.getHeapId(), newHeapSubscription.getSubscriptionId());

        final String sessionId = NioUtils.getSessionId(currentSession);
        ConnectedHeaps heaps;
        heapSubMutationLock.lock();
        try {
            heaps = heapsByServer.get(sessionId);
            if (heaps == null) {
                heaps = new ConnectedHeaps();
                heapsByServer.put(sessionId, heaps);
            }
        } finally {
            heapSubMutationLock.unlock();
        }

        // new heap
        boolean newHeap = false;
        if (newHeapSubscription.getUri() != null) {
            nioLogger.log(NioLogger.LoggingLevel.TRANSPORT, currentSession, "Received a new heap definition, heapId = %s, heapUrl = %s", newHeapSubscription.getHeapId(), newHeapSubscription.getUri());
            newHeap = heaps.addHeap(newHeapSubscription.getHeapId(), newHeapSubscription.getUri());
            if (!newHeap) {
                nioLogger.log(NioLogger.LoggingLevel.TRANSPORT, currentSession, "Received a new heap definition, heapId = %s, even though we know about the heap already!", newHeapSubscription.getHeapId());
            }
        }
        final boolean preExistingHeap = !newHeap;

        // find heap uri
        final HeapState heapState = heaps.getHeapState(newHeapSubscription.getHeapId());
        if (heapState == null) {
            nioLogger.log(NioLogger.LoggingLevel.TRANSPORT, currentSession, "Couldn't find heap definition, heapId = %s", newHeapSubscription.getHeapId());
            logger.log(Level.WARNING, "Can't find the heap for this subscription result. Heap id = " + newHeapSubscription.getHeapId());
            observer.onResult(new ExecutionResult(new CougarServiceException(ServerFaultCode.FrameworkError, "Can't find the heap for this subscription result. Heap id = " + newHeapSubscription.getHeapId())));
        } else {
            if (preExistingHeap && heapState.haveSeenInitialUpdate()) {
                Subscription sub = heapState.addSubscription(this, currentSession, newHeapSubscription.getHeapId(), newHeapSubscription.getSubscriptionId());
                if (sub != null) {
                    observer.onResult(new ExecutionResult(new ConnectedResponseImpl(heapState.getHeap(), sub)));
                } else {
                    // null sub means we already had a subscription with that id, something's not in a good state in the server, so kill this connection as we don't know what's going on
                    nioLogger.log(NioLogger.LoggingLevel.TRANSPORT, currentSession, "Duplicate subscription returned by the server, id = %s - closing session", newHeapSubscription.getSubscriptionId());
                    logger.log(Level.WARNING, "Duplicate subscription returned by the server, id = " + newHeapSubscription.getSubscriptionId() + " - closing session");
                    observer.onResult(new ExecutionResult(new CougarServiceException(ServerFaultCode.FrameworkError, "Duplicate subscription returned by the server, id = " + newHeapSubscription.getSubscriptionId())));
                    currentSession.close();
                }
            } else {
                // split this off into it's own thread since the mina docs lie and we only have one ioprocessor thread and if we don't fork we'd block forever
                final ConnectedHeaps finalHeaps = heaps;
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        boolean resultSent = false;
                        // now we've got the heap
                        CountDownLatch initialPopulationLatch = finalHeaps.getInitialPopulationLatch(newHeapSubscription.getHeapId());
                        try {
                            boolean populated = false;
                            if (initialPopulationLatch != null) {
                                nioLogger.log(NioLogger.LoggingLevel.TRANSPORT, currentSession, "Waiting for initial heap population, heapUrl = %s", newHeapSubscription.getUri());
                                populated = initialPopulationLatch.await(maxInitialPopulationWait, TimeUnit.MILLISECONDS);
                                finalHeaps.removeInitialPopulationLatch(newHeapSubscription.getHeapId());
                            } else {
                                nioLogger.log(NioLogger.LoggingLevel.TRANSPORT, currentSession, "Initial heap population, heapUrl = %s", newHeapSubscription.getUri());

                            }
                            nioLogger.log(NioLogger.LoggingLevel.TRANSPORT, currentSession, "Returning heap to client, heapUrl = %s", newHeapSubscription.getUri());
                            if (populated) {
                                observer.onResult(new ExecutionResult(new ConnectedResponseImpl(heapState.getHeap(), heapState.addSubscription(ClientConnectedObjectManager.this, currentSession, newHeapSubscription.getHeapId(), newHeapSubscription.getSubscriptionId()))));
                                resultSent = true;
                            }
                        } catch (InterruptedException e) {
                            // we got interrupted waiting for the response, oh well..
                        } catch (RuntimeException e) {
                            logger.log(Level.WARNING, "Error processing initial heap population, treating as a failure", e);
                        } finally {
                            if (!resultSent) {
                                nioLogger.log(NioLogger.LoggingLevel.TRANSPORT, currentSession, "Didn't get initial population message for heap, heapUrl = %s", newHeapSubscription.getUri());
                                // we don't worry about the case where it was a preExisting heap since the thread where it wasn't received will deal with it
                                if (!preExistingHeap) {
                                    terminateSubscriptions(currentSession, newHeapSubscription.getHeapId(), Subscription.CloseReason.INTERNAL_ERROR);
                                }
                                logger.log(Level.WARNING, "Didn't get initial population message for heap id = " + newHeapSubscription.getHeapId());
                                observer.onResult(new ExecutionResult(new CougarServiceException(ServerFaultCode.FrameworkError, "Didn't get initial population message for heap id = " + newHeapSubscription.getHeapId())));
                            }
                        }
                    }
                }, "SubscriptionResponseHandler-InitialPopulation-" + initialPopulationThreadIdSource.incrementAndGet() + "-" + heapState.getHeapUri()).start();
            }
View Full Code Here


    public void notifyObservers(int state) {
        synchronized (executionObservers) {
            for (WeakReference<ExecutionObserver> ref : executionObservers) {
                ExecutionObserver obs = ref.get();
                if (obs != null) {
                    obs.onResult(new ExecutionResult(new CougarServiceException(ServerFaultCode.JMSTransportCommunicationFailure, "Connection to Sonic has been lost")));
                }
            }
            executionObservers = new HashSet<WeakReference<ExecutionObserver>>();
        }
    }
View Full Code Here

        String destinationName = destinationResolver.resolveDestination(eventClass, null);
        String subId = null;

        if (destinationType == destinationType.DurableTopic) {
            if (args == null || "".equals(args[0])) {
                observer.onResult(new ExecutionResult(new CougarFrameworkException("Durable subscription requires a subscription Identifier to be set as the zeroth arg!")));
                return;
            }
            subId = args[0].toString();
        }

        try {
            final Session session = getConnection().createSession(false, acknowledgementMode);

            final TopicSubscriberPingMonitor pingMonitor = setupPingConsumer(eventName, destinationName, subId, session);

            MessageConsumer consumer = createConsumer(session, destinationName, subId);

            consumer.setMessageListener(new SubscriptionMessageListener(observer, eventClass, pingMonitor));

            subscriptionAdded(observer);

            observer.onResult(new ExecutionResult(new DefaultSubscription() {
                @Override
                public void preClose(CloseReason reason) {
                    try {
                        if (pingMonitor != null) {
                            if (monitorRegistry != null) {
                                monitorRegistry.removeMonitor(pingMonitor);
                            }
                            subscriberMonitorsBySession.remove(pingMonitor);
                        }
                        session.close();
                    } catch (JMSException ex) {
                        logger.log(Level.SEVERE, "Exception occurred when trying to close JMSConnection", ex);
                    }
                }
            }));
        } catch (JMSException ex) {
            observer.onResult(new ExecutionResult(new CougarFrameworkException("Subscription exception!", ex)));
        } catch (CougarException ce) {
            observer.onResult(new ExecutionResult(ce));
        }
    }
View Full Code Here

                    if (pingMonitor != null) {
                        pingMonitor.pingReceived((PingEvent) e);
                    }
                }
                else {
                    observer.onResult(new ExecutionResult(e));
                }
                message.acknowledge();
            } catch (Throwable t) { //NOSONAR
                handleThrowable(message, t);
            }
View Full Code Here

            String type = "null";
            if (m != null) {
                type = m.getClass().getName();
            }
            try {
                observer.onResult(new ExecutionResult(new CougarFrameworkException("Received message not a text message!",
                        new ClassCastException(
                                "Could not convert received message from type [" + type + "] to TextMessage"))));
            } catch (Exception e) {
                logger.log(e);
            }
View Full Code Here

            return;
        }

        CougarException ce = executionResult.getFault();
        if (ce instanceof CougarClientException) {
            wrapped.onResult(new ExecutionResult(new CougarServiceException(ServerFaultCode.ServiceRuntimeException,"Unhandled client exception",ce)));
        }
        else {
            wrapped.onResult(executionResult);
        }
    }
View Full Code Here

        if (result.getState().shouldInvoke()) {
            executionBody.run();
        } else {
            if (InterceptorState.FORCE_ON_EXCEPTION.equals(result.getState())) {
                Object interceptorResult = result.getResult();
                ExecutionResult executionResult;
                if (interceptorResult instanceof CougarException) {
                    executionResult = new ExecutionResult(interceptorResult);
                } else if (interceptorResult instanceof CougarApplicationException) {
                    executionResult = new ExecutionResult(interceptorResult);
                } else if (result.getResult() instanceof Exception) {
                    executionResult = new ExecutionResult(
                            new CougarFrameworkException(ServerFaultCode.ServiceRuntimeException,
                                    "Interceptor forced exception", (Exception)result.getResult()));
                } else {
                    // onException forced, but result is not an exception
                    executionResult = new ExecutionResult(
                            new CougarFrameworkException(ServerFaultCode.ServiceRuntimeException,
                                    "Interceptor forced exception, but result was not an exception - I found a " +
                                            result.getResult()));
                }
                observer.onResult(executionResult);

            } else if (InterceptorState.FORCE_ON_RESULT.equals(result.getState())) {
                observer.onResult(new ExecutionResult(result.getResult()));
            }
        }
    }
View Full Code Here

        }

        public void recreate(ExecutionObserver observer, ParameterType returnType, long size) {
            if (isSuccess()) {
                if (returnType.getImplementationClass().equals(EnumWrapper.class)) {
                    observer.onResult(new ExecutionResult(new EnumWrapper(returnType.getComponentTypes()[0].getImplementationClass(), (String)result)));
                }
                else {
                    observer.onResult(new ExecutionResult(result));
                }
            } else {
                observer.onResult(new ExecutionResult(exception));
            }
        }
View Full Code Here

        if (result.getState().shouldInvoke()) {
            executionBody.run();
        } else {
            if (InterceptorState.FORCE_ON_EXCEPTION.equals(result.getState())) {
                Object interceptorResult = result.getResult();
                ExecutionResult executionResult;
                if (interceptorResult instanceof CougarException) {
                    executionResult = new ExecutionResult((CougarException)interceptorResult);
                } else if (interceptorResult instanceof CougarApplicationException) {
                    executionResult = new ExecutionResult((CougarApplicationException)interceptorResult);
                } else if (result.getResult() instanceof Exception) {
                    executionResult = new ExecutionResult(
                            new CougarServiceException(ServerFaultCode.ServiceRuntimeException,
                                    "Interceptor forced exception", (Exception)result.getResult()));
                } else {
                    // onException forced, but result is not an exception
                    executionResult = new ExecutionResult(
                            new CougarServiceException(ServerFaultCode.ServiceRuntimeException,
                                    "Interceptor forced exception, but result was not an exception - I found a " +
                                            result.getResult()));
                }
                observer.onResult(executionResult);

            } else if (InterceptorState.FORCE_ON_RESULT.equals(result.getState())) {
                observer.onResult(new ExecutionResult(result.getResult()));
            }
        }
    }
View Full Code Here

                ServiceExceptionHandlingObserver exceptionHandlingObserver = new ServiceExceptionHandlingObserver(observer);

                try {
                    HealthSummaryResponse result = service.isHealthy((RequestContext)ctx, timeConstraints);
                    observer.onResult(new ExecutionResult(result));
                } catch (CougarException ce) {
                    exceptionHandlingObserver.onResult(new ExecutionResult(ce));
                } catch (HealthException ex) {
                    exceptionHandlingObserver.onResult(new ExecutionResult((CougarApplicationException)ex));
                };
            }
        });

      executableMap.put(HealthServiceDefinition.getDetailedHealthStatusKey,
            new Executable() {
                @Override
                public void execute(ExecutionContext ctx, OperationKey key,
                        Object[] args, ExecutionObserver observer,
                        ExecutionVenue executionVenue, TimeConstraints timeConstraints) {

                ServiceExceptionHandlingObserver exceptionHandlingObserver = new ServiceExceptionHandlingObserver(observer);

                try {
                    HealthDetailResponse result = service.getDetailedHealthStatus((RequestContext)ctx, timeConstraints);
                    observer.onResult(new ExecutionResult(result));
                } catch (CougarException ce) {
                    exceptionHandlingObserver.onResult(new ExecutionResult(ce));
                } catch (HealthException ex) {
                    exceptionHandlingObserver.onResult(new ExecutionResult((CougarApplicationException)ex));
                };
            }
        });

  }
View Full Code Here

TOP

Related Classes of com.betfair.cougar.core.api.ev.ExecutionResult

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.