Client Main.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
package com.kutay.zorlu.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 javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import com.kutay.zorlu.ejbremote.server.ExampleService; /** * Remote EJB Client. * @author Kutay ZORLU * */ 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-kutay/ExampleServiceImpl!com.kutay.zorlu.ejbremote.server.ExampleService"; # Module NAME! 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 // java.lang.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; } } |
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
<?xml version="1.0"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kutay.zorlu</groupId> <artifactId>ejb-remote-client-kutay</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>WildFly Example: EJB Remote Client</name> <description>Java client for remote EJB example on Wildfly</description> <url>http://www.kutayzorlu.com</url> <licenses> <license> <name>Microsoft License</name> <distribution>repo</distribution> <url>http://www.Microsoft.com/Licenses</url> </license> </licenses> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- JBoss dependency versions --> <version.wildfly>8.1.0.Final</version.wildfly> <version.jboss.spec.javaee.7.0>1.0.0.Final</version.jboss.spec.javaee.7.0> <!-- other plugin versions --> <version.compiler.plugin>3.1</version.compiler.plugin> <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> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.jboss.spec</groupId> <artifactId>jboss-javaee-7.0</artifactId> <version>${version.jboss.spec.javaee.7.0}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.wildfly</groupId> <artifactId>wildfly-ejb-client-bom</artifactId> <version>${version.wildfly}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- Business Interfaces of the server EJB. --> <dependency> <groupId>com.kutay.zorlu</groupId> <artifactId>ejb-remote-server-kutay</artifactId> <type>ejb</type> <version>1.0.0</version> </dependency> <!-- Import the transaction spec API, we use runtime scope because we aren't using any direct reference to the spec API in our client code --> <dependency> <groupId>org.jboss.spec.javax.transaction</groupId> <artifactId>jboss-transaction-api_1.2_spec</artifactId> <scope>runtime</scope> </dependency> <!-- Import the EJB 3.1 API, we use runtime scope because we aren't using any direct reference to EJB spec API in our client code --> <dependency> <groupId>org.jboss.spec.javax.ejb</groupId> <artifactId>jboss-ejb-api_3.2_spec</artifactId> <scope>runtime</scope> </dependency> <!-- JBoss EJB client API jar. We use runtime scope because the EJB client API isn't directly used in this example. We just need it in our runtime classpath --> <dependency> <groupId>org.jboss</groupId> <artifactId>jboss-ejb-client</artifactId> <scope>runtime</scope> </dependency> <!-- client communications with the server use XNIO --> <dependency> <groupId>org.jboss.xnio</groupId> <artifactId>xnio-api</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.jboss.xnio</groupId> <artifactId>xnio-nio</artifactId> <scope>runtime</scope> </dependency> <!-- The client needs JBoss remoting to access the server --> <dependency> <groupId>org.jboss.remoting</groupId> <artifactId>jboss-remoting</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.jboss</groupId> <artifactId>jboss-remote-naming</artifactId> <scope>runtime</scope> </dependency> <!-- Remote EJB accesses can be secured --> <dependency> <groupId>org.jboss.sasl</groupId> <artifactId>jboss-sasl</artifactId> <scope>runtime</scope> </dependency> <!-- data serialization for invoking remote EJBs --> <dependency> <groupId>org.jboss.marshalling</groupId> <artifactId>jboss-marshalling-river</artifactId> <scope>runtime</scope> </dependency> </dependencies> <build> <finalName>${project.artifactId}</finalName> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>${version.compiler.plugin}</version> <configuration> <source>${maven.compiler.source}</source> <target>${maven.compiler.target}</target> </configuration> </plugin> </plugins> </build> </project> |
# Controlling the result.
If you choose RMI, you don’t need a Java EE EJB app server. You have to keep client and server JVMs in synch; you can’t upgrade the client without upgrading the server. You have to write all the services that the EJB app server provides for you (e.g., connection pooling, naming and directory services, pooling, request queuing, transactions, etc.).
Agains the errors : You need to use JDK 8 –
1 2 |
No standard field found for reverse order comparator! at org.jboss.marshalling.river.Protocol.<clinit>(Protocol.java:220) |