Search in sources :

Example 6 with StatefulSessionComponent

use of org.jboss.as.ejb3.component.stateful.StatefulSessionComponent in project wildfly by wildfly.

the class AssociationImpl method receiveInvocationRequest.

@Override
public CancelHandle receiveInvocationRequest(@NotNull final InvocationRequest invocationRequest) {
    final EJBIdentifier ejbIdentifier = invocationRequest.getEJBIdentifier();
    final String appName = ejbIdentifier.getAppName();
    final String moduleName = ejbIdentifier.getModuleName();
    final String distinctName = ejbIdentifier.getDistinctName();
    final String beanName = ejbIdentifier.getBeanName();
    final EjbDeploymentInformation ejbDeploymentInformation = findEJB(appName, moduleName, distinctName, beanName);
    if (ejbDeploymentInformation == null) {
        invocationRequest.writeNoSuchEJB();
        return CancelHandle.NULL;
    }
    final ClassLoader classLoader = ejbDeploymentInformation.getDeploymentClassLoader();
    final InvocationRequest.Resolved requestContent;
    try {
        requestContent = invocationRequest.getRequestContent(classLoader);
    } catch (IOException | ClassNotFoundException e) {
        invocationRequest.writeException(new EJBException(e));
        return CancelHandle.NULL;
    }
    final Map<String, Object> attachments = requestContent.getAttachments();
    final EJBLocator<?> ejbLocator = requestContent.getEJBLocator();
    final String viewClassName = ejbLocator.getViewType().getName();
    if (!ejbDeploymentInformation.isRemoteView(viewClassName)) {
        invocationRequest.writeWrongViewType();
        return CancelHandle.NULL;
    }
    final ComponentView componentView = ejbDeploymentInformation.getView(viewClassName);
    final Method invokedMethod = findMethod(componentView, invocationRequest.getMethodLocator());
    if (invokedMethod == null) {
        invocationRequest.writeNoSuchMethod();
        return CancelHandle.NULL;
    }
    final boolean isAsync = componentView.isAsynchronous(invokedMethod);
    final boolean oneWay = isAsync && invokedMethod.getReturnType() == void.class;
    if (oneWay) {
        // send immediate response
        requestContent.writeInvocationResult(null);
    }
    final CancellationFlag cancellationFlag = new CancellationFlag();
    Runnable runnable = () -> {
        if (!cancellationFlag.runIfNotCancelled()) {
            if (!oneWay)
                invocationRequest.writeCancelResponse();
            return;
        }
        // invoke the method
        final Object result;
        // the Remoting connection that is set here is only used for legacy purposes
        SecurityActions.remotingContextSetConnection(invocationRequest.getProviderInterface(Connection.class));
        try {
            result = invokeMethod(componentView, invokedMethod, invocationRequest, requestContent, cancellationFlag);
        } catch (EJBComponentUnavailableException ex) {
            // if the EJB is shutting down when the invocation was done, then it's as good as the EJB not being available. The client has to know about this as
            // a "no such EJB" failure so that it can retry the invocation on a different node if possible.
            EjbLogger.EJB3_INVOCATION_LOGGER.debugf("Cannot handle method invocation: %s on bean: %s due to EJB component unavailability exception. Returning a no such EJB available message back to client", invokedMethod, beanName);
            if (!oneWay)
                invocationRequest.writeNoSuchEJB();
            return;
        } catch (ComponentIsStoppedException ex) {
            EjbLogger.EJB3_INVOCATION_LOGGER.debugf("Cannot handle method invocation: %s on bean: %s due to EJB component stopped exception. Returning a no such EJB available message back to client", invokedMethod, beanName);
            if (!oneWay)
                invocationRequest.writeNoSuchEJB();
            return;
        // TODO should we write a specifc response with a specific protocol letting client know that server is suspending?
        } catch (CancellationException ex) {
            if (!oneWay)
                invocationRequest.writeCancelResponse();
            return;
        } catch (Exception exception) {
            if (oneWay)
                return;
            // write out the failure
            final Exception exceptionToWrite;
            final Throwable cause = exception.getCause();
            if (componentView.getComponent() instanceof StatefulSessionComponent && exception instanceof EJBException && cause != null) {
                if (!(componentView.getComponent().isRemotable(cause))) {
                    // Avoid serializing the cause of the exception in case it is not remotable
                    // Client might not be able to deserialize and throw ClassNotFoundException
                    exceptionToWrite = new EJBException(exception.getLocalizedMessage());
                } else {
                    exceptionToWrite = exception;
                }
            } else {
                exceptionToWrite = exception;
            }
            invocationRequest.writeException(exceptionToWrite);
            return;
        }
        // invocation was successful
        if (!oneWay)
            try {
                // attach any weak affinity if available
                Affinity weakAffinity = null;
                if (ejbLocator.isStateful() && componentView.getComponent() instanceof StatefulSessionComponent) {
                    final StatefulSessionComponent statefulSessionComponent = (StatefulSessionComponent) componentView.getComponent();
                    weakAffinity = getWeakAffinity(statefulSessionComponent, ejbLocator.asStateful());
                } else if (componentView.getComponent() instanceof StatelessSessionComponent) {
                    final StatelessSessionComponent statelessSessionComponent = (StatelessSessionComponent) componentView.getComponent();
                    weakAffinity = statelessSessionComponent.getWeakAffinity();
                }
                if (weakAffinity != null && !weakAffinity.equals(Affinity.NONE)) {
                    attachments.put(Affinity.WEAK_AFFINITY_CONTEXT_KEY, weakAffinity);
                }
                requestContent.writeInvocationResult(result);
            } catch (Throwable ioe) {
                EjbLogger.REMOTE_LOGGER.couldNotWriteMethodInvocation(ioe, invokedMethod, beanName, appName, moduleName, distinctName);
            }
    };
    // invoke the method and write out the response, possibly on a separate thread
    execute(invocationRequest, runnable, isAsync);
    return cancellationFlag::cancel;
}
Also used : ComponentIsStoppedException(org.jboss.as.ee.component.ComponentIsStoppedException) StatelessSessionComponent(org.jboss.as.ejb3.component.stateless.StatelessSessionComponent) EJBComponentUnavailableException(org.jboss.as.ejb3.component.EJBComponentUnavailableException) EjbDeploymentInformation(org.jboss.as.ejb3.deployment.EjbDeploymentInformation) InvocationRequest(org.jboss.ejb.server.InvocationRequest) StatefulSessionComponent(org.jboss.as.ejb3.component.stateful.StatefulSessionComponent) IOException(java.io.IOException) Method(java.lang.reflect.Method) EJBComponentUnavailableException(org.jboss.as.ejb3.component.EJBComponentUnavailableException) CancellationException(java.util.concurrent.CancellationException) EJBException(javax.ejb.EJBException) ComponentIsStoppedException(org.jboss.as.ee.component.ComponentIsStoppedException) IOException(java.io.IOException) ComponentView(org.jboss.as.ee.component.ComponentView) CancellationException(java.util.concurrent.CancellationException) Affinity(org.jboss.ejb.client.Affinity) CancellationFlag(org.jboss.as.ejb3.component.interceptors.CancellationFlag) EJBException(javax.ejb.EJBException) EJBIdentifier(org.jboss.ejb.client.EJBIdentifier)

Example 7 with StatefulSessionComponent

use of org.jboss.as.ejb3.component.stateful.StatefulSessionComponent in project wildfly by wildfly.

the class AssociationImpl method receiveSessionOpenRequest.

@Override
@NotNull
public CancelHandle receiveSessionOpenRequest(@NotNull final SessionOpenRequest sessionOpenRequest) {
    final EJBIdentifier ejbIdentifier = sessionOpenRequest.getEJBIdentifier();
    final String appName = ejbIdentifier.getAppName();
    final String moduleName = ejbIdentifier.getModuleName();
    final String beanName = ejbIdentifier.getBeanName();
    final String distinctName = ejbIdentifier.getDistinctName();
    final EjbDeploymentInformation ejbDeploymentInformation = findEJB(appName, moduleName, distinctName, beanName);
    if (ejbDeploymentInformation == null) {
        sessionOpenRequest.writeNoSuchEJB();
        return CancelHandle.NULL;
    }
    final Component component = ejbDeploymentInformation.getEjbComponent();
    if (!(component instanceof StatefulSessionComponent)) {
        sessionOpenRequest.writeNotStateful();
        return CancelHandle.NULL;
    }
    final StatefulSessionComponent statefulSessionComponent = (StatefulSessionComponent) component;
    // generate the session id and write out the response, possibly on a separate thread
    final AtomicBoolean cancelled = new AtomicBoolean();
    Runnable runnable = () -> {
        if (cancelled.get()) {
            sessionOpenRequest.writeCancelResponse();
            return;
        }
        final SessionID sessionID;
        try {
            sessionID = statefulSessionComponent.createSessionRemote();
        } catch (Exception t) {
            EjbLogger.REMOTE_LOGGER.exceptionGeneratingSessionId(t, statefulSessionComponent.getComponentName(), ejbIdentifier);
            sessionOpenRequest.writeException(t);
            return;
        }
        sessionOpenRequest.convertToStateful(sessionID);
    };
    execute(sessionOpenRequest, runnable, false);
    return ignored -> cancelled.set(true);
}
Also used : DeploymentRepositoryListener(org.jboss.as.ejb3.deployment.DeploymentRepositoryListener) NotNull(org.wildfly.common.annotation.NotNull) ModuleDeployment(org.jboss.as.ejb3.deployment.ModuleDeployment) ClusterTopologyListener(org.jboss.ejb.server.ClusterTopologyListener) SessionBeanComponent(org.jboss.as.ejb3.component.session.SessionBeanComponent) Future(java.util.concurrent.Future) EJBComponentUnavailableException(org.jboss.as.ejb3.component.EJBComponentUnavailableException) Map(java.util.Map) Affinity(org.jboss.ejb.client.Affinity) SecurityIdentity(org.wildfly.security.auth.server.SecurityIdentity) DeploymentModuleIdentifier(org.jboss.as.ejb3.deployment.DeploymentModuleIdentifier) InvocationRequest(org.jboss.ejb.server.InvocationRequest) Method(java.lang.reflect.Method) StatefulSessionComponent(org.jboss.as.ejb3.component.stateful.StatefulSessionComponent) Registry(org.wildfly.clustering.registry.Registry) CancellationException(java.util.concurrent.CancellationException) StatefulEJBLocator(org.jboss.ejb.client.StatefulEJBLocator) Set(java.util.Set) InterceptorContext(org.jboss.invocation.InterceptorContext) Collectors(java.util.stream.Collectors) ClientMapping(org.jboss.as.network.ClientMapping) EJBException(javax.ejb.EJBException) ModuleAvailabilityListener(org.jboss.ejb.server.ModuleAvailabilityListener) List(java.util.List) EjbDeploymentInformation(org.jboss.as.ejb3.deployment.EjbDeploymentInformation) EJBLocator(org.jboss.ejb.client.EJBLocator) Request(org.jboss.ejb.server.Request) ComponentIsStoppedException(org.jboss.as.ee.component.ComponentIsStoppedException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Connection(org.jboss.remoting3.Connection) HashMap(java.util.HashMap) EJBIdentifier(org.jboss.ejb.client.EJBIdentifier) ArrayList(java.util.ArrayList) StatelessSessionComponent(org.jboss.as.ejb3.component.stateless.StatelessSessionComponent) DeploymentRepository(org.jboss.as.ejb3.deployment.DeploymentRepository) SessionOpenRequest(org.jboss.ejb.server.SessionOpenRequest) CancellationFlag(org.jboss.as.ejb3.component.interceptors.CancellationFlag) Executor(java.util.concurrent.Executor) Association(org.jboss.ejb.server.Association) ComponentView(org.jboss.as.ee.component.ComponentView) IOException(java.io.IOException) InvocationType(org.jboss.as.ee.component.interceptors.InvocationType) EJBMethodLocator(org.jboss.ejb.client.EJBMethodLocator) SessionID(org.jboss.ejb.client.SessionID) EjbLogger(org.jboss.as.ejb3.logging.EjbLogger) ListenerHandle(org.jboss.ejb.server.ListenerHandle) EJBClientInvocationContext(org.jboss.ejb.client.EJBClientInvocationContext) Component(org.jboss.as.ee.component.Component) CancelHandle(org.jboss.ejb.server.CancelHandle) Collections(java.util.Collections) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EjbDeploymentInformation(org.jboss.as.ejb3.deployment.EjbDeploymentInformation) StatefulSessionComponent(org.jboss.as.ejb3.component.stateful.StatefulSessionComponent) SessionBeanComponent(org.jboss.as.ejb3.component.session.SessionBeanComponent) StatefulSessionComponent(org.jboss.as.ejb3.component.stateful.StatefulSessionComponent) StatelessSessionComponent(org.jboss.as.ejb3.component.stateless.StatelessSessionComponent) Component(org.jboss.as.ee.component.Component) EJBIdentifier(org.jboss.ejb.client.EJBIdentifier) SessionID(org.jboss.ejb.client.SessionID) EJBComponentUnavailableException(org.jboss.as.ejb3.component.EJBComponentUnavailableException) CancellationException(java.util.concurrent.CancellationException) EJBException(javax.ejb.EJBException) ComponentIsStoppedException(org.jboss.as.ee.component.ComponentIsStoppedException) IOException(java.io.IOException) NotNull(org.wildfly.common.annotation.NotNull)

Example 8 with StatefulSessionComponent

use of org.jboss.as.ejb3.component.stateful.StatefulSessionComponent in project wildfly by wildfly.

the class LocalEjbReceiver method createSession.

@Override
protected <T> StatefulEJBLocator<T> createSession(StatelessEJBLocator<T> statelessLocator, NamingProvider namingProvider) throws Exception {
    final EjbDeploymentInformation ejbInfo = findBean(statelessLocator);
    final EJBComponent component = ejbInfo.getEjbComponent();
    if (!(component instanceof StatefulSessionComponent)) {
        throw EjbLogger.ROOT_LOGGER.notStatefulSessionBean(statelessLocator.getAppName(), statelessLocator.getModuleName(), statelessLocator.getDistinctName(), statelessLocator.getBeanName());
    }
    final StatefulSessionComponent statefulComponent = (StatefulSessionComponent) component;
    final SessionID sessionID = statefulComponent.createSession();
    return statelessLocator.withSession(sessionID);
}
Also used : EjbDeploymentInformation(org.jboss.as.ejb3.deployment.EjbDeploymentInformation) StatefulSessionComponent(org.jboss.as.ejb3.component.stateful.StatefulSessionComponent) EJBComponent(org.jboss.as.ejb3.component.EJBComponent) SessionID(org.jboss.ejb.client.SessionID)

Aggregations

StatefulSessionComponent (org.jboss.as.ejb3.component.stateful.StatefulSessionComponent)4 SessionID (org.jboss.ejb.client.SessionID)4 EJBException (javax.ejb.EJBException)3 Component (org.jboss.as.ee.component.Component)3 ComponentView (org.jboss.as.ee.component.ComponentView)3 EJBComponent (org.jboss.as.ejb3.component.EJBComponent)3 EjbDeploymentInformation (org.jboss.as.ejb3.deployment.EjbDeploymentInformation)3 IOException (java.io.IOException)2 Method (java.lang.reflect.Method)2 CancellationException (java.util.concurrent.CancellationException)2 ComponentIsStoppedException (org.jboss.as.ee.component.ComponentIsStoppedException)2 EJBComponentUnavailableException (org.jboss.as.ejb3.component.EJBComponentUnavailableException)2 CancellationFlag (org.jboss.as.ejb3.component.interceptors.CancellationFlag)2 StatelessSessionComponent (org.jboss.as.ejb3.component.stateless.StatelessSessionComponent)2 Affinity (org.jboss.ejb.client.Affinity)2 EJBIdentifier (org.jboss.ejb.client.EJBIdentifier)2 InvocationRequest (org.jboss.ejb.server.InvocationRequest)2 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1