Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ To compile and run the example you can simply use a maven call:
cd client
mvn clean compile package exec:java

You should see the example result "Hello World" and the the remotely queried Wildfly Home Dir on the output.
You should see the example result "Hello World" and the the remotely queried Wildfly Home Dir on the output.

#Contributor

- @ryankarl65
14 changes: 12 additions & 2 deletions client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
<version.exec.plugin>1.2.1</version.exec.plugin>

<!-- maven-compiler-plugin -->
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<maven.compiler.source>8</maven.compiler.source>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -127,8 +127,18 @@
<artifactId>jboss-marshalling-river</artifactId>
<scope>runtime</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.12.1</version>
</dependency>

</dependencies>



<build>
<finalName>${project.artifactId}</finalName>
<plugins>
Expand Down
216 changes: 77 additions & 139 deletions client/src/main/java/com/illucit/ejbremote/EjbRemoteClient.java
Original file line number Diff line number Diff line change
@@ -1,154 +1,92 @@
package com.illucit.ejbremote;

import static javax.naming.Context.INITIAL_CONTEXT_FACTORY;
import static javax.naming.Context.PROVIDER_URL;
import static javax.naming.Context.URL_PKG_PREFIXES;

import java.util.Hashtable;
import java.util.Map;
import com.illucit.ejbremote.server.ExampleService;
import com.illucit.ejbremote.utils.EjbUrlUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Map;

import com.illucit.ejbremote.server.ExampleService;
import static com.illucit.ejbremote.utils.ContextUtils.createEjbProxy;
import static com.illucit.ejbremote.utils.ContextUtils.createRemoteEjbContext;

/**
* Remote EJB Client.
*
* @author Christian Simon
*
* @author Christian Simon
*/
public class EjbRemoteClient {

/**
* Run example.
*
* @param args
* (not used)
*/
public static void main(String[] args) {

// Connection to Wildfly Server instance
String host = "127.0.0.1";
String port = "8080"; // Wildfly HTTP port

Context remotingContext;
try {
remotingContext = createRemoteEjbContext(host, port);
} catch (NamingException e) {
System.err.println("Error setting up remoting context");
e.printStackTrace();
return;
}

// Syntax: ejb:${appName}/${moduleName}/${beanName}!${remoteView}
// appName = name of EAR deployment (or empty for single EJB/WAR
// deployments)
// moduleName = name of EJB/WAR deployment
// beanName = name of the EJB (Simple name of EJB class)
// remoteView = fully qualified remote interface class
String ejbUrl = "ejb:/ejb-remote-server/ExampleServiceImpl!com.illucit.ejbremote.server.ExampleService";

ExampleService service;
try {
service = createEjbProxy(remotingContext, ejbUrl, ExampleService.class);
} catch (NamingException e) {
System.err.println("Error resolving bean");
e.printStackTrace();
return;
} catch (ClassCastException e) {
System.err.println("Resolved EJB is of wrong type");
e.printStackTrace();
return;
}

// Call remote method with parameter

String toGreet = "World";

String exampleResult;
try {
exampleResult = service.greet(toGreet);
} catch (Exception e) {
System.err.println("Error accessing remote bean");
e.printStackTrace();
return;
}

// Hello World!
System.out.println("Example result: " + exampleResult);

// Retrieve result from EJB call

Map<Object, Object> systemProperties;
try {
systemProperties = service.getSystemProperties();
} catch (Exception e) {
System.err.println("Error accessing remote bean");
e.printStackTrace();
return;
}

System.out.println("Wildfly Home Dir: " + systemProperties.get("jboss.home.dir"));

}

/**
* Create Remote EJB Context.
*
* @param host
* host to connect to (e.g. "127.0.0.1")
* @param port
* port to connect to (wildfly HTTP port, e.g. 8080)
* @return remote EJB context
* @throws NamingException
* if creating the context fails
*/
private static Context createRemoteEjbContext(String host, String port) throws NamingException {

Hashtable<Object, Object> props = new Hashtable<>();

props.put(INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
props.put(URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");

props.put("jboss.naming.client.ejb.context", false);
props.put("org.jboss.ejb.client.scoped.context", true);

props.put("endpoint.name", "client-endpoint");
props.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED", false);
props.put("remote.connections", "default");
props.put("remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS", false);

props.put(PROVIDER_URL, "http-remoting://" + host + ":" + port);
props.put("remote.connection.default.host", host);
props.put("remote.connection.default.port", port);

return new InitialContext(props);
}

/**
* Get a proxy for a remote EJB.
*
* @param remotingContext
* remote EJB context
* @param ejbUrl
* URL of the EJB
* @param ejbInterfaceClass
* class of the remote interface of the EJB
* @param <T>
* type of the EJB remote interface
* @return EJB proxy
* @throws NamingException
* if the name resolving fails
* @throws ClassCastException
* if the EJB proxy is not of the given type
*/
@SuppressWarnings("unchecked")
private static <T> T createEjbProxy(Context remotingContext, String ejbUrl, Class<T> ejbInterfaceClass)
throws NamingException, ClassCastException {
Object resolvedproxy = remotingContext.lookup(ejbUrl);
return (T) resolvedproxy;
}
/**
* Run example.
*
* @param args (not used)
*/
public static void main(String[] args) {

Logger log = LogManager.getLogger(EjbRemoteClient.class);

// Connection to Wildfly Server instance
String host = "127.0.0.1";
String port = "8080"; // Wildfly HTTP port

Context remotingContext;
try {
remotingContext = createRemoteEjbContext(host, port);
} catch (NamingException e) {
log.error("Error setting up remoting context");
return;
}


final String appName = "ejb-remote-server";
final String moduleName = "";
final String beanName = "ExampleServiceImpl";
final String remoteInterface = "com.illucit.ejbremote.server.ExampleService";

String ejbUrl = EjbUrlUtils.getEjbUrl(appName, moduleName, beanName, remoteInterface);

ExampleService service = null;
try {
service = createEjbProxy(remotingContext, ejbUrl, ExampleService.class);
log.info(" Connexion to Remote is Done !");
} catch (NamingException e) {
log.error("Error resolving bean");
} catch (ClassCastException e) {
log.error("Resolved EJB is of wrong type");
}

// Call remote method with parameter

String toGreet = "World";

String exampleResult;
try {
assert service != null;
exampleResult = service.greet(toGreet);
} catch (Exception e) {
log.error("Error accessing remote bean");
return;
}

// Hello World!
System.out.println("Example result: " + exampleResult);

// Retrieve result from EJB call

Map<Object, Object> systemProperties;
try {
systemProperties = service.getSystemProperties();
} catch (Exception e) {
log.error("Error accessing remote bean");
return;
}

System.out.println("Wildfly Home Dir: " + systemProperties.get("jboss.home.dir"));

}


}
59 changes: 59 additions & 0 deletions client/src/main/java/com/illucit/ejbremote/utils/ContextUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.illucit.ejbremote.utils;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Hashtable;

import static javax.naming.Context.*;

public class ContextUtils {

/**
* Create Remote EJB Context.
*
* @param host host to connect to (e.g. "127.0.0.1")
* @param port port to connect to (wildfly HTTP port, e.g. 8080)
* @return remote EJB context
* @throws NamingException if creating the context fails
*/
public static Context createRemoteEjbContext(String host, String port) throws NamingException {

Hashtable<Object, Object> props = new Hashtable<>();

props.put(INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
props.put(URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");

props.put("jboss.naming.client.ejb.context", false);
props.put("org.jboss.ejb.client.scoped.context", true);

props.put("endpoint.name", "client-endpoint");
props.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED", false);
props.put("remote.connections", "default");
props.put("remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS", false);

props.put(PROVIDER_URL, "http-remoting://" + host + ":" + port);
props.put("remote.connection.default.host", host);
props.put("remote.connection.default.port", port);

return new InitialContext(props);
}

/**
* Get a proxy for a remote EJB.
*
* @param remotingContext remote EJB context
* @param ejbUrl URL of the EJB
* @param ejbInterfaceClass class of the remote interface of the EJB
* @param <T> type of the EJB remote interface
* @return EJB proxy
* @throws NamingException if the name resolving fails
* @throws ClassCastException if the EJB proxy is not of the given type
*/
@SuppressWarnings("unchecked")
public static <T> T createEjbProxy(Context remotingContext, String ejbUrl, Class<T> ejbInterfaceClass)
throws NamingException, ClassCastException {
Object resolvedproxy = remotingContext.lookup(ejbUrl);
return (T) resolvedproxy;
}
}
27 changes: 27 additions & 0 deletions client/src/main/java/com/illucit/ejbremote/utils/EjbUrlUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.illucit.ejbremote.utils;

public class EjbUrlUtils {

/**
* Configure EJB Url
* Syntax: ejb:${appName}/${moduleName}/${beanName}!${remoteView}
*
* @param appName name of EAR deployment (or empty for single EJB/WAR deployments)
* @param moduleName name of EJB/WAR deployment
* @param beanName name of the EJB (Simple name of EJB class)
* @param remoteInterface fully qualified remote interface class
* @return String
* @author Meudje Karl
*/
public static String getEjbUrl(String appName, String moduleName, String beanName, String remoteInterface) {

return "ejb:/" +
appName +
"/" +
moduleName +
"/" +
beanName +
"!" +
remoteInterface;
}
}