public ApplicationClientStartService(final Method mainMethod, final String[] parameters, final InjectedEENamespaceContextSelector namespaceContextSelectorInjectedValue, final ClassLoader classLoader, final List<SetupAction> setupActions, final EJBClientConfiguration configuration) { this.mainMethod = mainMethod; this.parameters = parameters; this.namespaceContextSelectorInjectedValue = namespaceContextSelectorInjectedValue; this.classLoader = classLoader; this.hostUrl = null; this.setupActions = setupActions; this.contextSelector = new ConfigBasedEJBClientContextSelector(configuration); } @Override
@Override public void reconnect() throws IOException { Connection connection = null; try { final IoFuture<Connection> futureConnection = NetworkUtil.connect(endpoint, destinationHost, destinationPort, null, connectionCreationOptions, callbackHandler, null); connection = IoFutureHelper.get(futureConnection, connectionTimeout, TimeUnit.MILLISECONDS); logger.debug("Successfully reconnected to connection " + connection); } catch (Exception e) { logger.debug("Failed to re-connect to " + this.destinationHost + ":" + this.destinationPort, e); } if (connection == null) { return; } try { final EJBReceiver ejbReceiver = new RemotingConnectionEJBReceiver(connection, this, channelCreationOptions); RemotingConnectionClusterNodeManager.this.clusterContext.registerEJBReceiver(ejbReceiver); } finally { // if we successfully re-connected then unregister this ReconnectHandler from the EJBClientContext RemotingConnectionClusterNodeManager.this.clusterContext.getEJBClientContext().unregisterReconnectHandler(this); } } }
protected void writeAttachments(final ObjectOutput output, final Map<String, Object> attachments) throws IOException { if (attachments == null) { output.writeByte(0); return; } // write the attachment count PackedInteger.writePackedInteger(output, attachments.size()); for (Map.Entry<String, Object> entry : attachments.entrySet()) { output.writeObject(entry.getKey()); output.writeObject(entry.getValue()); } }
@Override public void reconnect() throws IOException { this.reconnectAttemptCount++; final ServiceController serviceController = this.serviceRegistry.getService(this.outboundConnectionServiceName); if (serviceController == null) { // the outbound connection service is no longer available, so unregister this // reconnect handler from the EJB client context logger.debug("Unregistering " + this + " since " + this.outboundConnectionServiceName + " is no longer available"); this.clientContext.unregisterReconnectHandler(this); return; } final AbstractOutboundConnectionService outboundConnectionService = (AbstractOutboundConnectionService) serviceController.getValue(); try { final IoFuture<Connection> futureConnection = outboundConnectionService.connect(); final Connection connection = IoFutureHelper.get(futureConnection, connectionTimeout, TimeUnit.MILLISECONDS); logger.debug("Successful reconnect attempt#" + this.reconnectAttemptCount + " to outbound connection " + this.outboundConnectionServiceName); // successfully reconnected so unregister this reconnect handler this.clientContext.unregisterReconnectHandler(this); // register the newly reconnected connection final EJBReceiver receiver = new RemotingConnectionEJBReceiver(connection, this, channelCreationOpts); this.clientContext.registerEJBReceiver(receiver); } catch (Exception e) { logger.debug("Reconnect attempt#" + this.reconnectAttemptCount + " failed for outbound connection " + this.outboundConnectionServiceName, e); } } }
private synchronized void createConnection() { try { endpoint = Remoting.createEndpoint("endpoint", OptionMap.EMPTY); endpoint.addConnectionProvider("remote", new RemoteConnectionProviderFactory(), OptionMap.create(Options.SSL_ENABLED, Boolean.FALSE)); // open a connection final IoFuture<Connection> futureConnection = endpoint.connect(new URI(hostUrl), OptionMap.create(Options.SASL_POLICY_NOANONYMOUS, Boolean.FALSE, Options.SASL_POLICY_NOPLAINTEXT, Boolean.FALSE), callbackHandler); connection = IoFutureHelper.get(futureConnection, 30L, TimeUnit.SECONDS); final EJBClientContext ejbClientContext = EJBClientContext.create(); ejbClientContext.registerConnection(connection); this.clientContext = ejbClientContext; } catch (IOException e) { throw new RuntimeException(e); } catch (URISyntaxException e) { throw new RuntimeException(e); } }
public JBossLookupConfiguration(Properties jbossEjbClientProperties) { EJBClientConfiguration ejbClientConfiguration = new PropertiesBasedEJBClientConfiguration(jbossEjbClientProperties); ContextSelector<EJBClientContext> ejbClientContextSelector = new ConfigBasedEJBClientContextSelector(ejbClientConfiguration); EJBClientContext.setSelector(ejbClientContextSelector); }
final IoFuture<Connection> futureConnection = NetworkUtil.connect(endpoint, destinationHost, destinationPort, null, connectionCreationOptions, callbackHandler, null); connection = IoFutureHelper.get(futureConnection, 5000, TimeUnit.MILLISECONDS); final OptionMap connectionCreationOptions = clusterNodeConfiguration == null ? clusterConfiguration.getConnectionCreationOptions() : clusterNodeConfiguration.getConnectionCreationOptions(); final CallbackHandler callbackHandler = clusterNodeConfiguration == null ? clusterConfiguration.getCallbackHandler() : clusterNodeConfiguration.getCallbackHandler(); final IoFuture<Connection> futureConnection = NetworkUtil.connect(endpoint, destinationHost, destinationPort, null, connectionCreationOptions, callbackHandler, null); final long timeout = clusterNodeConfiguration == null ? clusterConfiguration.getConnectionTimeout() : clusterNodeConfiguration.getConnectionTimeout(); connection = IoFutureHelper.get(futureConnection, timeout, TimeUnit.MILLISECONDS); final IoFuture<Connection> futureConnection = NetworkUtil.connect(endpoint, destinationHost, destinationPort, null, connectionCreationOptions, callbackHandler, null); connection = IoFutureHelper.get(futureConnection, 5000, TimeUnit.MILLISECONDS); return null; return new RemotingConnectionEJBReceiver(connection, reconnectHandler, channelCreationOptions);
private void registerRemotingEJBReceivers(final StartContext startContext, final EJBClientContext context) { final ServiceRegistry serviceRegistry = startContext.getController().getServiceContainer(); int numRemotingReceivers = 0; for (final Map.Entry<ServiceName, InjectedValue<AbstractOutboundConnectionService>> entry : this.remotingOutboundConnections.entrySet()) { final InjectedValue<AbstractOutboundConnectionService> injectedValue = entry.getValue(); final AbstractOutboundConnectionService outboundConnectionService = injectedValue.getValue(); final String connectionName = outboundConnectionService.getConnectionName(); logger.debug("Creating remoting EJB receiver for connection " + connectionName); final long connectionTimeout = this.connectionTimeouts.get(connectionName) <= 0 ? DEFAULT_CONNECTION_TIMEOUT : this.connectionTimeouts.get(connectionName); final OptionMap options = this.channelCreationOpts.get(connectionName) == null ? OptionMap.EMPTY : this.channelCreationOpts.get(connectionName); Connection connection = null; final ReconnectHandler reconnectHandler = new OutboundConnectionReconnectHandler(serviceRegistry, entry.getKey(), context, connectionTimeout, options); try { final IoFuture<Connection> futureConnection = outboundConnectionService.connect(); connection = IoFutureHelper.get(futureConnection, connectionTimeout, TimeUnit.MILLISECONDS); } catch (Exception e) { // just log a message and register a reconnect handler logger.debug("Failed to create a connection for " + connectionName + ". A reconnect handler will be added to the client context", e); context.registerReconnectHandler(reconnectHandler); continue; } final RemotingConnectionEJBReceiver ejbReceiver = new RemotingConnectionEJBReceiver(connection, reconnectHandler, options); context.registerEJBReceiver(ejbReceiver); numRemotingReceivers++; } logger.debug("Added " + numRemotingReceivers + " remoting EJB receivers to descriptor based EJB client context " + startContext.getController().getName()); }
void writeNodesRemoved(final DataOutput output, final String clusterName, final Set<String> removedNodes) throws IOException { if (output == null) { throw EjbMessages.MESSAGES.cannotWriteToNullDataOutput(); } if (removedNodes == null || removedNodes.isEmpty()) { return; } // write the header output.write(HEADER_NODES_REMOVED); // write the cluster count PackedInteger.writePackedInteger(output, 1); // write the cluster name output.writeUTF(clusterName); // write the removed nodes count final int removedNodesCount = removedNodes.size(); PackedInteger.writePackedInteger(output, removedNodesCount); // write out the member info for each removed member for (final String node : removedNodes) { // write the node name output.writeUTF(node); } }
private void initializeEJBClientContext() { Properties properties = new Properties(); properties.put("endpoint.name", "client-endpoint"); properties.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED", "false"); properties.put("remote.connections", "default"); properties.put("remote.connection.default.host", "localhost"); properties.put("remote.connection.default.port", "4447"); properties.put("remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS", "false"); PropertiesBasedEJBClientConfiguration configuration = new PropertiesBasedEJBClientConfiguration(properties); final ContextSelector<EJBClientContext> ejbClientContextSelector = new ConfigBasedEJBClientContextSelector(configuration); final ContextSelector<EJBClientContext> previousSelector = EJBClientContext.setSelector(ejbClientContextSelector); }
private void writeModuleReport(final DataOutput output, final DeploymentModuleIdentifier[] modules) throws IOException { // write the count PackedInteger.writePackedInteger(output, modules.length); // write the module identifiers for (int i = 0; i < modules.length; i++) { // write the app name final String appName = modules[i].getApplicationName(); if (appName == null) { // write out a empty string output.writeUTF(""); } else { output.writeUTF(appName); } // write the module name output.writeUTF(modules[i].getModuleName()); // write the distinct name final String distinctName = modules[i].getDistinctName(); if (distinctName == null) { // write out an empty string output.writeUTF(""); } else { output.writeUTF(distinctName); } } } }
public JBossLookupConfiguration(Properties jbossEjbClientProperties) { EJBClientConfiguration ejbClientConfiguration = new PropertiesBasedEJBClientConfiguration(jbossEjbClientProperties); ContextSelector<EJBClientContext> ejbClientContextSelector = new ConfigBasedEJBClientContextSelector(ejbClientConfiguration); EJBClientContext.setSelector(ejbClientContextSelector); }
void writeNewNodesAdded(final DataOutput output, final String clusterName, final Map<String, List<ClientMapping>> clientMappings) throws IOException { if (output == null) { throw EjbMessages.MESSAGES.cannotWriteToNullDataOutput(); } if (clientMappings.isEmpty()) { return; } // write the header output.write(HEADER_NEW_NODES_ADDED); // write the cluster count PackedInteger.writePackedInteger(output, 1); // write the cluster name output.writeUTF(clusterName); // write out the cluster node(s) information this.writeClusterNodes(output, clusterName, clientMappings); }
@Test public void canLookup_testFacadeBean_usingEjbClientApi_withoutFile() throws Exception { // Set EJB Client API properties programmatically instead of using // jboss-ejb-client.properties file Properties clientProp = new Properties(); clientProp.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED", "false"); clientProp.put("remote.connections", "default"); clientProp.put("remote.connection.default.port", "18080"); clientProp.put("remote.connection.default.host", "localhost"); // clientProp.put("remote.connection.default.username", "ejbUser"); // clientProp.put("remote.connection.default.password", "ejbPassword"); clientProp.put("remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS", "false"); // Set selector (avoiding usage of jboss-ejb-client.properties file) EJBClientConfiguration cc = new PropertiesBasedEJBClientConfiguration(clientProp); ContextSelector<EJBClientContext> selector = new ConfigBasedEJBClientContextSelector(cc); EJBClientContext.setSelector(selector); final Hashtable<String, String> jndiProperties = new Hashtable<>(); // jndiProperties.put(Context.PROVIDER_URL, "remote://localhost:14447"); jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming"); final Context context = new InitialContext(jndiProperties); // Important: We need to use the 'ejb:' prefix... TestRemote testRemote = (TestRemote) context.lookup("ejb:/test//TestRemoteInterfaceBean!" + TestRemote.class.getName()); Assert.assertEquals("TestRemoteBean says hello to EjbClientApi", testRemote.getGreeting("EjbClientApi")); }
/** * Writes out a EJB remoting protocol message containing the names of the <code>clusters</code> which have been removed * from the server. * * @param output The {@link DataOutput} to which the message will be written * @param registries * @throws IOException */ void writeClusterRemoved(final DataOutput output, final Collection<Registry<String, List<ClientMapping>>> registries) throws IOException { if (output == null) { throw EjbMessages.MESSAGES.cannotWriteToNullDataOutput(); } if (registries == null || registries.isEmpty()) { return; } // write the header output.write(HEADER_CLUSTER_REMOVED); // write the cluster count PackedInteger.writePackedInteger(output, registries.size()); // write out the cluster name for each of the removed cluster for (final Registry<String, List<ClientMapping>> registry : registries) { output.writeUTF(registry.getName()); } }
protected void writeTxPrepareResponseMessage(final ChannelAssociation channelAssociation, final short invocationId, final int xaResourceStatusCode) throws IOException { final DataOutputStream dataOutputStream; final MessageOutputStream messageOutputStream; try { messageOutputStream = channelAssociation.acquireChannelMessageOutputStream(); } catch (Exception e) { throw EjbMessages.MESSAGES.failedToOpenMessageOutputStream(e); } dataOutputStream = new DataOutputStream(messageOutputStream); try { // write header dataOutputStream.writeByte(HEADER_TX_INVOCATION_RESPONSE); // write invocation id dataOutputStream.writeShort(invocationId); // write a "bit" to indicate that this message contains the XAResource status for a "prepare" // invocation dataOutputStream.writeBoolean(true); // write the XAResource status PackedInteger.writePackedInteger(dataOutputStream, xaResourceStatusCode); } finally { channelAssociation.releaseChannelMessageOutputStream(messageOutputStream); dataOutputStream.close(); } }
private void sendVersionMessage(final ChannelAssociation channelAssociation) throws IOException { final DataOutputStream outputStream; final MessageOutputStream messageOutputStream; try { messageOutputStream = channelAssociation.acquireChannelMessageOutputStream(); } catch (Exception e) { throw EjbMessages.MESSAGES.failedToOpenMessageOutputStream(e); } outputStream = new DataOutputStream(messageOutputStream); try { // write the version outputStream.write(this.serverProtocolVersion); // write the marshaller type count PackedInteger.writePackedInteger(outputStream, this.supportedMarshallingStrategies.length); // write the marshaller types for (int i = 0; i < this.supportedMarshallingStrategies.length; i++) { outputStream.writeUTF(this.supportedMarshallingStrategies[i]); } } finally { channelAssociation.releaseChannelMessageOutputStream(messageOutputStream); outputStream.close(); } }
private void writeClusterNodes(final DataOutput output, final String clusterName, final Map<String, List<ClientMapping>> clientMappings) throws IOException { PackedInteger.writePackedInteger(output, memberCount); throw EjbMessages.MESSAGES.clientMappingMissing(nodeName, clusterName); PackedInteger.writePackedInteger(output, clientMappingsForNode.size()); PackedInteger.writePackedInteger(output, netMaskAndIPFamilyDifferentiator);
/** * Writes out a EJB remoting protocol message containing the cluster topology information for the passed <code>clusters</code> * * @param output The {@link DataOutput} into which the message will be written * @param clientMappingsRegistries * @throws IOException */ void writeCompleteClusterTopology(final DataOutput output, final Collection<Registry<String, List<ClientMapping>>> clientMappingsRegistries) throws IOException { if (output == null) { throw EjbMessages.MESSAGES.cannotWriteToNullDataOutput(); } if (clientMappingsRegistries == null || clientMappingsRegistries.isEmpty()) { return; } // write the header output.write(HEADER_COMPLETE_CLUSTER_TOPOLOGY); // write the cluster count PackedInteger.writePackedInteger(output, clientMappingsRegistries.size()); // write out each of the cluster's topology for (final Registry<String, List<ClientMapping>> registry : clientMappingsRegistries) { // write the cluster name output.writeUTF(registry.getName()); // write out the information of each cluster node this.writeClusterNodes(output, registry.getName(), registry.getEntries()); } }
private void writeSessionId(final ChannelAssociation channelAssociation, final short invocationId, final SessionID sessionID, final Affinity hardAffinity) throws IOException { final byte[] sessionIdBytes = sessionID.getEncodedForm(); final DataOutputStream dataOutputStream; final MessageOutputStream messageOutputStream; try { messageOutputStream = channelAssociation.acquireChannelMessageOutputStream(); } catch (Exception e) { throw EjbMessages.MESSAGES.failedToOpenMessageOutputStream(e); } dataOutputStream = new DataOutputStream(messageOutputStream); try { // write out header dataOutputStream.writeByte(HEADER_SESSION_OPEN_RESPONSE); // write out invocation id dataOutputStream.writeShort(invocationId); // session id byte length PackedInteger.writePackedInteger(dataOutputStream, sessionIdBytes.length); // write out the session id bytes dataOutputStream.write(sessionIdBytes); // now marshal the hard affinity associated with this session final Marshaller marshaller = this.prepareForMarshalling(this.marshallerFactory, dataOutputStream); marshaller.writeObject(hardAffinity); // finish marshalling marshaller.finish(); } finally { channelAssociation.releaseChannelMessageOutputStream(messageOutputStream); dataOutputStream.close(); } }