private static <T> void writeRawIdentifier(final EJBLocator<T> statelessLocator, final MessageOutputStream out) throws IOException { final String appName = statelessLocator.getAppName(); out.writeUTF(appName == null ? "" : appName); out.writeUTF(statelessLocator.getModuleName()); final String distinctName = statelessLocator.getDistinctName(); out.writeUTF(distinctName == null ? "" : distinctName); out.writeUTF(statelessLocator.getBeanName()); }
@Override public String toString() { return String.format("%s for \"%s\", view is %s, affinity is %s", getClass().getSimpleName(), identifier, getViewType(), getAffinity()); }
void setSessionID(final SessionID sessionID) { final AtomicReference<EJBLocator<T>> locatorRef = this.locatorRef; EJBLocator<T> oldVal, newVal; do { oldVal = locatorRef.get(); if (oldVal.isStateful()) { if (oldVal.asStateful().getSessionId().equals(sessionID)) { // harmless/idempotent return; } throw Logs.MAIN.ejbIsAlreadyStateful(); } newVal = oldVal.withSession(sessionID); } while (! locatorRef.compareAndSet(oldVal, newVal)); }
void setStrongAffinity(final Affinity newAffinity) { final AtomicReference<EJBLocator<T>> locatorRef = this.locatorRef; EJBLocator<T> oldVal, newVal; do { oldVal = locatorRef.get(); if (oldVal.getAffinity().equals(newAffinity)) { return; } newVal = oldVal.withNewAffinity(newAffinity); } while (! locatorRef.compareAndSet(oldVal, newVal)); }
return Integer.valueOf(locatorRef.get().hashCode()); if (locatorRef.get().isEntity()) { return locatorRef.get().narrowAsEntity(EJBObject.class).getPrimaryKey(); return EJBHandle.create(locatorRef.get().narrowTo(EJBObject.class)); return EJBHomeHandle.create(locatorRef.get().narrowAsHome(EJBHome.class)); Logs.INVOCATION.debugf("Calling invoke(module = %s, strong affinity = %s, weak affinity = %s): ", locatorRef.get().getIdentifier(), locatorRef.get().getAffinity(), weakAffinity);
nodeName = selector.selectNode(nodes.values().toArray(NO_STRINGS), locator.getAppName(), locator.getModuleName(), locator.getDistinctName()); if (nodeName == null) { throw Logs.INVOCATION.selectorReturnedNull(selector);
/** * Construct a new instance. This constructor uses the location from the original locator, but with the given * session ID. * * @param original the original locator * @param sessionId the session ID */ public StatefulEJBLocator(final EJBLocator<T> original, final SessionID sessionId) { super(original, original.getAffinity()); Assert.checkNotNullParam("sessionId", sessionId); this.sessionId = sessionId; }
public Object invoke(final Object rawProxy, final Method method, final Object... args) throws Exception { final T proxy = locatorRef.get().getViewType().cast(rawProxy); final EJBProxyInformation.ProxyMethodInfo methodInfo = locatorRef.get().getProxyInformation().getProxyMethodInfo(method); return invoke(proxy, methodInfo, args); }
private void updateAffinities(InvocationRequest invocationRequest, Map<String, Object> attachments, EJBLocator<?> ejbLocator, ComponentView componentView) { Affinity legacyAffinity = null; Affinity weakAffinity = null; Affinity clusterAffinity = getClusterAffinity(); if (ejbLocator.isStateful() && componentView.getComponent() instanceof StatefulSessionComponent) { final StatefulSessionComponent statefulSessionComponent = (StatefulSessionComponent) componentView.getComponent(); weakAffinity = legacyAffinity = getWeakAffinity(statefulSessionComponent, ejbLocator.asStateful()); } else if (componentView.getComponent() instanceof StatelessSessionComponent) { // V3 and less used cluster affinity as a weak affinity for SLSBs legacyAffinity = clusterAffinity; } // Always use the cluster as the strong affinity, if there is one if (clusterAffinity != null) { invocationRequest.updateStrongAffinity(clusterAffinity); } if (weakAffinity != null && !weakAffinity.equals(Affinity.NONE)) { invocationRequest.updateWeakAffinity(weakAffinity); } if (legacyAffinity != null && !legacyAffinity.equals(Affinity.NONE)) { attachments.put(Affinity.WEAK_AFFINITY_CONTEXT_KEY, legacyAffinity); } }
public Object handleInvocationResult(final EJBClientInvocationContext context) throws Exception { final Object result; try { result = context.getResult(); } catch (NoSuchEJBException | RequestSendFailedException e) { processMissingTarget(context); throw e; } final EJBLocator<?> locator = context.getLocator(); if (locator.isStateful() && locator.getAffinity() instanceof ClusterAffinity && context.getWeakAffinity() == Affinity.NONE) { // set the weak affinity to the location of the session (in case it failed over) final Affinity targetAffinity = context.getTargetAffinity(); if (targetAffinity != null) { context.setWeakAffinity(targetAffinity); } else { final URI destination = context.getDestination(); if (destination != null) { context.setWeakAffinity(URIAffinity.forUri(destination)); } // if destination is null, then an interceptor set the location } } return result; }
/** * Get the invoked view class. * * @return the invoked view class */ public Class<?> getViewClass() { return locator.getViewType(); }
protected SessionID createSession(final EJBReceiverSessionCreationContext context) throws Exception { final StatelessEJBLocator<?> statelessLocator = context.getClientInvocationContext().getLocator().asStateless(); final AuthenticationContext authenticationContext = context.getAuthenticationContext(); try { IoFuture<ConnectionPeerIdentity> futureConnection = getConnection(context.getClientInvocationContext(), context.getClientInvocationContext().getDestination(), authenticationContext); final ConnectionPeerIdentity identity = futureConnection.getInterruptibly(); final EJBClientChannel ejbClientChannel = getClientChannel(identity.getConnection()); final StatefulEJBLocator<?> result = ejbClientChannel.openSession(statelessLocator, identity, context.getClientInvocationContext()); return result.getSessionId(); } catch (IOException e) { final RequestSendFailedException failed = new RequestSendFailedException("Failed to create stateful EJB: " + e.getMessage(), true); failed.initCause(e); throw failed; } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new CreateException("Stateful EJB creation interrupted"); } }
private List<Throwable> executeDiscovery(AbstractInvocationContext context) { assert context.getDestination() == null; final EJBLocator<?> locator = context.getLocator(); final Affinity affinity = locator.getAffinity(); final Affinity weakAffinity = context.getWeakAffinity(); filterSpec = getFilterSpec(locator.getIdentifier().getModuleIdentifier()); return doAnyDiscovery(context, filterSpec, locator);
private static <T extends EJBObject, H extends EJBHome> StatefulEJBMetaData<T, ? extends H> createStatefulMetaData(Class<T> remoteClass, Class<H> homeClass, EJBHome home) { return new StatefulEJBMetaData<>(remoteClass, EJBClient.getLocatorFor(home).<H>narrowAsHome(homeClass)); }
return Integer.valueOf(locatorRef.get().hashCode()); if (locatorRef.get().isEntity()) { return locatorRef.get().narrowAsEntity(EJBObject.class).getPrimaryKey(); return EJBHandle.create(locatorRef.get().narrowTo(EJBObject.class)); return EJBHomeHandle.create(locatorRef.get().narrowAsHome(EJBHome.class)); Logs.INVOCATION.debugf("Calling invoke(module = %s, strong affinity = %s, weak affinity = %s): ", locatorRef.get().getIdentifier(), locatorRef.get().getAffinity(), weakAffinity);
nodeName = selector.selectNode(nodes.values().toArray(NO_STRINGS), locator.getAppName(), locator.getModuleName(), locator.getDistinctName()); if (nodeName == null) { throw Logs.INVOCATION.selectorReturnedNull(selector);
/** * Construct a new asynchronous instance. * * @param other the synchronous invocation handler */ EJBInvocationHandler(final EJBInvocationHandler<T> other) { super(other); final EJBLocator<T> locator = other.locatorRef.get(); locatorRef = new AtomicReference<>(locator); authenticationContextSupplier = other.authenticationContextSupplier; async = true; if (locator instanceof StatefulEJBLocator) { // set the weak affinity to the node on which the session was created setWeakAffinity(locator.getAffinity()); } }
boolean compareAndSetStrongAffinity(final Affinity expectedAffinity, final Affinity newAffinity) { Assert.checkNotNullParam("expectedAffinity", expectedAffinity); Assert.checkNotNullParam("newAffinity", newAffinity); final AtomicReference<EJBLocator<T>> locatorRef = this.locatorRef; EJBLocator<T> oldVal = locatorRef.get(); if (! oldVal.getAffinity().equals(expectedAffinity)) { return false; } EJBLocator<T> newVal = oldVal.withNewAffinity(newAffinity); return locatorRef.compareAndSet(oldVal, newVal); } }
public Object invoke(final Object rawProxy, final Method method, final Object... args) throws Exception { final T proxy = locatorRef.get().getViewType().cast(rawProxy); final EJBProxyInformation.ProxyMethodInfo methodInfo = locatorRef.get().getProxyInformation().getProxyMethodInfo(method); return invoke(proxy, methodInfo, args); }
if (ejbLocator.isStateful()) { interceptorContext.putPrivateData(SessionID.class, ejbLocator.asStateful().getSessionId());