Skip to content

Commit

Permalink
[EJBCLIENT-186] Introduce blocking discovery provider for Remoting, a…
Browse files Browse the repository at this point in the history
…long with preconfigured connection list
  • Loading branch information
dmlloyd committed Jan 18, 2017
1 parent 9808d83 commit c6d1488
Show file tree
Hide file tree
Showing 6 changed files with 252 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
import org.wildfly.common.Assert;

/**
* A one-time, configuration-based EJB client context selector.
* A one-time, configuration-based EJB client context configurator.
*
* @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
*/
public final class ConfigurationBasedEJBClientContextSelector implements Supplier<EJBClientContext> {
final class ConfigurationBasedEJBClientContextSelector implements Supplier<EJBClientContext> {
private static final EJBClientContext configuredContext;

private static final String NS_EJB_CLIENT_3_0 = "urn:jboss:ejb-client:3.0";
Expand All @@ -67,6 +67,7 @@ private static EJBClientContext loadConfiguration() {
} catch (ConfigXMLParseException e) {
throw new IllegalStateException(e);
}
// TODO: parse ejb-client.properties instead right here
// build a generic config instead
final EJBClientContext.Builder builder = new EJBClientContext.Builder();
loadTransportProviders(builder, classLoader);
Expand Down Expand Up @@ -228,6 +229,9 @@ private static void parseConnectionType(final ConfigurationXMLStreamReader strea
streamReader.skipContent();
}
} else if (next == END_ELEMENT) {
final EJBClientConnection.Builder connBuilder = new EJBClientConnection.Builder();
connBuilder.setDestination(uri);
builder.addClientConnection(connBuilder.build());
return;
} else {
throw Assert.unreachableCode();
Expand Down
78 changes: 78 additions & 0 deletions src/main/java/org/jboss/ejb/client/EJBClientConnection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2017 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jboss.ejb.client;

import java.net.URI;

import org.wildfly.common.Assert;

/**
* Information about a configured connection on an EJB client context.
*
* @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
*/
public final class EJBClientConnection {
private final URI destination;

EJBClientConnection(final Builder builder) {
destination = builder.destination;
}

/**
* Get the connection destination URI.
*
* @return the connection destination URI (not {@code null})
*/
public URI getDestination() {
return destination;
}

/**
* A builder for a client connection definition.
*/
public static final class Builder {
URI destination;

/**
* Construct a new instance.
*/
public Builder() {
}

/**
* Set the destination URI.
*
* @param destination the destination URI (must not be {@code null})
*/
public void setDestination(final URI destination) {
Assert.checkNotNullParam("destination", destination);
this.destination = destination;
}

/**
* Build a new {@link EJBClientConnection} instance based on the current contents of this builder.
*
* @return the new instance (not {@code null})
*/
public EJBClientConnection build() {
Assert.checkNotNullParam("destination", destination);
return new EJBClientConnection(this);
}
}
}
39 changes: 28 additions & 11 deletions src/main/java/org/jboss/ejb/client/EJBClientContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;

Expand All @@ -37,7 +38,6 @@
import org.wildfly.common.context.Contextual;
import org.wildfly.discovery.Discovery;
import org.wildfly.discovery.FilterSpec;
import org.wildfly.discovery.ServiceRegistry;
import org.wildfly.discovery.ServiceType;
import org.wildfly.discovery.ServicesQueue;

Expand Down Expand Up @@ -106,8 +106,8 @@ public final class EJBClientContext extends Attachable implements Contextual<EJB
private final EJBClientInterceptor[] interceptors;
private final EJBTransportProvider[] transportProviders;
private final long invocationTimeout;
private final ServiceRegistry serviceRegistry;
private final EJBReceiverContext receiverContext;
private final List<EJBClientConnection> configuredConnections;

EJBClientContext(Builder builder) {
final List<EJBClientInterceptor> builderInterceptors = builder.interceptors;
Expand All @@ -122,9 +122,16 @@ public final class EJBClientContext extends Attachable implements Contextual<EJB
} else {
transportProviders = builderTransportProviders.toArray(new EJBTransportProvider[builderTransportProviders.size()]);
}
serviceRegistry = builder.serviceRegistry;
invocationTimeout = 0;
receiverContext = new EJBReceiverContext(this);
final List<EJBClientConnection> clientConnections = builder.clientConnections;
if (clientConnections == null || clientConnections.isEmpty()) {
configuredConnections = Collections.emptyList();
} else if (clientConnections.size() == 1) {
configuredConnections = Collections.singletonList(clientConnections.get(0));
} else {
configuredConnections = Collections.unmodifiableList(new ArrayList<>(clientConnections));
}
// this must be last
for (EJBTransportProvider transportProvider : transportProviders) {
transportProvider.notifyRegistered(receiverContext);
Expand Down Expand Up @@ -158,6 +165,16 @@ public long getInvocationTimeout() {
return invocationTimeout;
}

/**
* Get the pre-configured connections for this context. This information may not be used by some transport providers
* and mainly exists for legacy compatibility purposes.
*
* @return the pre-configured connections for this context (not {@code null})
*/
public List<EJBClientConnection> getConfiguredConnections() {
return configuredConnections;
}

/**
* Get a copy of this context with the given interceptor(s) added. If the array is {@code null} or empty, the
* current context is returned as-is.
Expand Down Expand Up @@ -231,18 +248,14 @@ Discovery getDiscovery() {
return DISCOVERY_SUPPLIER.get();
}

ServiceRegistry getServiceRegistry() {
return serviceRegistry;
}

/**
* A builder for EJB client contexts.
*/
public static final class Builder {

List<EJBClientInterceptor> interceptors;
List<EJBTransportProvider> transportProviders;
ServiceRegistry serviceRegistry = doPrivileged((PrivilegedAction<ServiceRegistry>) ServiceRegistry.getContextManager()::get);
List<EJBClientConnection> clientConnections;

/**
* Construct a new instance.
Expand All @@ -259,6 +272,7 @@ public Builder() {
if (transportProviders.length > 0) {
this.transportProviders = new ArrayList<>(Arrays.asList(transportProviders));
}
clientConnections = new ArrayList<>(ejbClientContext.getConfiguredConnections());
}

public void addInterceptor(EJBClientInterceptor interceptor) {
Expand All @@ -277,9 +291,12 @@ public void addTransportProvider(EJBTransportProvider provider) {
transportProviders.add(provider);
}

public void setServiceRegistry(final ServiceRegistry serviceRegistry) {
Assert.checkNotNullParam("serviceRegistry", serviceRegistry);
this.serviceRegistry = serviceRegistry;
public void addClientConnection(EJBClientConnection connection) {
Assert.checkNotNullParam("connection", connection);
if (clientConnections == null) {
clientConnections = new ArrayList<>();
}
clientConnections.add(connection);
}

public EJBClientContext build() {
Expand Down
11 changes: 0 additions & 11 deletions src/main/java/org/jboss/ejb/client/EJBReceiverContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

package org.jboss.ejb.client;

import org.wildfly.discovery.ServiceRegistry;

/**
* A context which is provided to EJB receiver implementations in order to perform operations on the client context.
*
Expand All @@ -32,15 +30,6 @@ public final class EJBReceiverContext {
this.clientContext = clientContext;
}

/**
* Get the discovery service registry to use for server discovery events.
*
* @return the discovery service registry
*/
public ServiceRegistry getServiceRegistry() {
return clientContext.getServiceRegistry();
}

/**
* Get the client context that corresponds to this receiver context.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@
import org.wildfly.discovery.ServiceRegistration;
import org.wildfly.discovery.ServiceRegistry;
import org.wildfly.discovery.ServiceURL;
import org.wildfly.discovery.impl.LocalRegistryAndDiscoveryProvider;
import org.wildfly.discovery.spi.DiscoveryProvider;
import org.wildfly.security.auth.AuthenticationException;
import org.wildfly.transaction.client.ContextTransactionManager;
import org.wildfly.transaction.client.LocalTransaction;
Expand Down Expand Up @@ -122,6 +124,7 @@ class EJBClientChannel {

private final RemoteTransactionContext transactionContext;
private final AtomicReference<FutureResult<EJBClientChannel>> futureResultRef;
private final LocalRegistryAndDiscoveryProvider discoveryProvider = new LocalRegistryAndDiscoveryProvider();

EJBClientChannel(final Channel channel, final int version, final FutureResult<EJBClientChannel> futureResult) {
this.channel = channel;
Expand All @@ -139,7 +142,7 @@ class EJBClientChannel {
// server does not present v3 unless the transaction service is also present
}
transactionContext = RemoteTransactionContext.getInstance();
this.serviceRegistry = REGISTRY_SUPPLIER.get();
this.serviceRegistry = ServiceRegistry.create(discoveryProvider);
this.configuration = configuration;
invocationTracker = new InvocationTracker(this.channel, channel.getOption(RemotingOptions.MAX_OUTBOUND_MESSAGES).intValue(), EJBClientChannel::mask);
futureResultRef = new AtomicReference<>(futureResult);
Expand Down Expand Up @@ -791,6 +794,10 @@ UserTransactionID allocateUserTransactionID() {
}
}

DiscoveryProvider getDiscoveryProvider() {
return discoveryProvider;
}

final class MethodInvocation extends Invocation {
private final EJBReceiverInvocationContext receiverInvocationContext;
private final AtomicInteger refCounter = new AtomicInteger(1);
Expand Down
Loading

0 comments on commit c6d1488

Please sign in to comment.