I am trying to connect to a Remote EJB that is deployed in a JBoss 7 Server. I tried to figure out what the JNDI name is by looking at the JNDI dump that I got from the JBoss CLI.
No matter what I try I cannot lookup the EJB.
I think that the jndi name should be: java:global/XNet/api/ReceivingAPI_EJB!com.mycompany.receiving.api.ReceivingAPI_EJBRemote
Here is the client that I am using:
package com.mycompany.mavenproject1;
import com.mycompany.receiving.api.ReceivingAPI_EJBRemote;
import com.mycompany.receiving.api.ReceivingAPI;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Properties;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args ) throws NamingException
{
System.out.println( "Hello World!" );
//ReceivingAPI ejbRemote = DomainHelper.getHQApi(com.mycompany.receiving.api.ReceivingAPI.class);
ReceivingAPI_EJBRemote ejbRemote = App.lookupRemoteStatelessCalculator();
ejbRemote.getOpenRcvdocForSite(null, 7);
}
// private static ReceivingAPI_EJBRemote lookupRemoteStatelessReceiving() throws NamingException {
//
// }
//
private static ReceivingAPI_EJBRemote lookupRemoteStatelessCalculator() throws NamingException {
final Properties jndiProperties = new Properties();
jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
jndiProperties.put(Context.PROVIDER_URL,"remote://someserver.xmx.com:1199");
// username
jndiProperties.put(Context.SECURITY_PRINCIPAL, "mycompany1");
// password
jndiProperties.put(Context.SECURITY_CREDENTIALS, "<removed>");
//final Context context = new InitialContext(jndiProperties);
jndiProperties.put("jboss.naming.client.ejb.context", true);
InitialContext context = new InitialContext( jndiProperties );
// The app name is the application name of the deployed EJBs. This is typically the ear name
// without the .ear suffix. However, the application name could be overridden in the application.xml of the
// EJB deployment on the server.
// Since we haven't deployed the application as a .ear, the app name for us will be an empty string
final String appName = "XNet";
// This is the module name of the deployed EJBs on the server. This is typically the jar name of the
// EJB deployment, without the .jar suffix, but can be overridden via the ejb-jar.xml
// In this example, we have deployed the EJBs in a jboss-as-ejb-remote-app.jar, so the module name is
// jboss-as-ejb-remote-app
final String moduleName = "api";
// AS7 allows each deployment to have an (optional) distinct name. We haven't specified a distinct name for
// our EJB deployment, so this is an empty string
final String distinctName = "hq";
// The EJB name which by default is the simple class name of the bean implementation class
final String beanName = com.mycompany.receiving.api.ReceivingAPI.class.getSimpleName();
// the remote view fully qualified class name
final String viewClassName = ReceivingAPI_EJBRemote.class.getName();
// let's do the lookup
try {
ReceivingAPI test1 = (ReceivingAPI)context.lookup("java:global/XNet/api/" + beanName + "_EJB!" + viewClassName);
System.out.println("Test1 = " + test1.getClass().getName());
} catch(Throwable t) {
System.out.println(t);
}
try {
ReceivingAPI test = (ReceivingAPI)context.lookup("java:global/XNet/api/hq/" + beanName + "_EJB!com.mycompany.receiving.api.ReceivingAPI_EJBRemote");
System.out.println("Test = " + test.getClass().getName());
} catch(Throwable t) {
System.out.println(t);
}
try {
ReceivingAPI test = (ReceivingAPI)context.lookup("ejb:XNet/api/hq/" + beanName + "_EJB!com.mycompany.receiving.api.ReceivingAPI_EJBRemote");
System.out.println("Test = " + test.getClass().getName());
} catch(Throwable t) {
System.out.println(t);
}
return (ReceivingAPI_EJBRemote) context.lookup("ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "_EJB!" + viewClassName);
}
}
The output from the run of the previous java class:
Hello World!
javax.naming.NameNotFoundException: global/XNet/api/ReceivingAPI_EJB!com.mycompany.receiving.api.ReceivingAPI_EJBRemote -- service jboss.naming.context.java.jboss.exported.global.XNet.api."ReceivingAPI_EJB!com.mycompany.receiving.api.ReceivingAPI_EJBRemote"
javax.naming.NameNotFoundException: global/XNet/api/hq/ReceivingAPI_EJB!com.mycompany.receiving.api.ReceivingAPI_EJBRemote -- service jboss.naming.context.java.jboss.exported.global.XNet.api.hq."ReceivingAPI_EJB!com.mycompany.receiving.api.ReceivingAPI_EJBRemote"
javax.naming.NameNotFoundException: ejb:XNet/api/hq/ReceivingAPI_EJB!com.mycompany.receiving.api.ReceivingAPI_EJBRemote -- service jboss.naming.context.java.jboss.exported.ejb:XNet.api.hq."ReceivingAPI_EJB!com.mycompany.receiving.api.ReceivingAPI_EJBRemote"
Exception in thread "main" javax.naming.NameNotFoundException: ejb:XNet/api/hq/ReceivingAPI_EJB!com.mycompany.receiving.api.ReceivingAPI_EJBRemote -- service jboss.naming.context.java.jboss.exported.ejb:XNet.api.hq."ReceivingAPI_EJB!com.mycompany.receiving.api.ReceivingAPI_EJBRemote"
at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:97)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:178)
at org.jboss.naming.remote.protocol.v1.Protocol$1.handleServerMessage(Protocol.java:127)
at org.jboss.naming.remote.protocol.v1.RemoteNamingServerV1$MessageReciever$1.run(RemoteNamingServerV1.java:73)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)
Here is the dump of the JNDI CLI Command:
[jboss1#hmudev01 bin]$ ./jboss-cli.sh --connect controller=localhost:10099
[standalone#localhost:10099 /] /subsystem=naming:jndi-view
{
"outcome" => "success",
"result" => {
"java: contexts" => {
"java:global" => {
"XNet" => {
"class-name" => "javax.naming.Context",
"children" => {
"api" => {
"class-name" => "javax.naming.Context",
"children" => {
"ReceivingAPI_EJB!com.mycompany.receiving.api.ReceivingAPI_EJBLocal" => {
"class-name" => "com.mycompany.receiving.api.ReceivingAPI_EJBLocal$$$view46",
"value" => "Proxy for view class: com.mycompany.receiving.api.ReceivingAPI_EJBLocal of EJB: ReceivingAPI_EJB"
},
"ReceivingAPI_EJB!com.mycompany.receiving.api.ReceivingAPI_EJBRemote" => {
"class-name" => "com.sun.proxy.$Proxy272",
"value" => "Proxy for remote EJB StatelessEJBLocator{appName='XNet', moduleName='api', distinctName='hq', beanName='ReceivingAPI_EJB', view='interface com.mycompany.receiving.api.ReceivingAPI_EJBRemote'}"
},
}
}
}
}
}
}
...
}
}
[standalone#localhost:10099 /]
Any my maven deps are as follows:
<dependencies>
<dependency>
<groupId>com.mycompany.receiving</groupId>
<artifactId>xnet-domain-receiving-client</artifactId>
<version>2.5.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-naming</artifactId>
<version>7.1.3.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.marshalling</groupId>
<artifactId>jboss-marshalling-river</artifactId>
<version>1.4.0.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.client</groupId>
<artifactId>jbossall-client</artifactId>
<version>5.0.0.GA</version>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.ejb</groupId>
<artifactId>jboss-ejb-api_3.1_spec</artifactId>
<version>1.0.2.Final</version>
</dependency>
</dependencies>
In the comment above Rhys was right about the jndi string looking "funky". I tried everything I thought. Here is what worked for my situation.
The JNDI URL needs to look like this:
XNet/api/ReceivingAPI_EJB!com.mycompany.receiving.api.ReceivingAPI_EJBRemote
I also had to add add a dependency to the others shown in the question:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
Related
i am a new bee to GCP and trying to develop springboot rest api that connects to GCP Bigtable, do we have a quickstart guide that helps in development.
Yup, there is. Check out this link to get started. You need the Java client library.
https://cloud.google.com/bigtable/docs/reference/libraries#client-libraries-install-java
for Maven:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>26.1.4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-bigtable</artifactId>
</dependency>
Then check this for usage:
https://cloud.google.com/bigtable/docs/reference/libraries#client-libraries-usage-java
import com.google.api.gax.rpc.NotFoundException;
import com.google.cloud.bigtable.data.v2.BigtableDataClient;
import com.google.cloud.bigtable.data.v2.BigtableDataSettings;
import com.google.cloud.bigtable.data.v2.models.Row;
import com.google.cloud.bigtable.data.v2.models.RowCell;
public class Quickstart {
public static void main(String... args) {
String projectId = args[0]; // my-gcp-project-id
String instanceId = args[1]; // my-bigtable-instance-id
String tableId = args[2]; // my-bigtable-table-id
quickstart(projectId, instanceId, tableId);
}
public static void quickstart(String projectId, String instanceId, String tableId) {
BigtableDataSettings settings =
BigtableDataSettings.newBuilder().setProjectId(projectId).setInstanceId(instanceId).build();
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (BigtableDataClient dataClient = BigtableDataClient.create(settings)) {
System.out.println("\nReading a single row by row key");
Row row = dataClient.readRow(tableId, "r1");
System.out.println("Row: " + row.getKey().toStringUtf8());
for (RowCell cell : row.getCells()) {
System.out.printf(
"Family: %s Qualifier: %s Value: %s%n",
cell.getFamily(), cell.getQualifier().toStringUtf8(), cell.getValue().toStringUtf8());
}
} catch (NotFoundException e) {
System.err.println("Failed to read from a non-existent table: " + e.getMessage());
} catch (Exception e) {
System.out.println("Error during quickstart: \n" + e.toString());
}
}
}
I am using one of the JBoss JMS quickstart projects (HelloWorld-JMS) which uses JMS to write to a message queue. The queue has been defined in the JBoss XML configuration file. Our application runs perfectly fine when we run it from the IDE. However, when we package it as a JAR, we start seeing the exception:
SEVERE: Exception NamingException: javax.naming.InvalidNameException: WFNAM00007: Invalid URL scheme name "http-remoting"
We have also tried:
http-remoting://127.0.0.1:8080
remoting+http://127.0.0.1:8080
But we get the same exception.
Here's the main part of my code:
private static final String DEFAULT_MESSAGE = "Hello, World!";
private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
private static final String DEFAULT_DESTINATION = "jms/queue/myqueue";
private static final String DEFAULT_MESSAGE_COUNT = "1";
private static final String DEFAULT_USERNAME = "john";
private static final String DEFAULT_PASSWORD = "doe";
private static final String INITIAL_CONTEXT_FACTORY = "org.wildfly.naming.client.WildFlyInitialContextFactory";
private static final String PROVIDER_URL = "http-remoting://127.0.0.1:8080";
Context namingContext = null;
String userName = System.getProperty("username", DEFAULT_USERNAME);
String password = System.getProperty("password", DEFAULT_PASSWORD);
// Set up the namingContext for the JNDI lookup
final Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
env.put(Context.PROVIDER_URL, System.getProperty(Context.PROVIDER_URL, PROVIDER_URL));
env.put(Context.SECURITY_PRINCIPAL, userName);
env.put(Context.SECURITY_CREDENTIALS, password);
namingContext = new InitialContext(env);
// Perform the JNDI lookups
String connectionFactoryString = System.getProperty("connection.factory", DEFAULT_CONNECTION_FACTORY);
log.info("Attempting to acquire connection factory \"" + connectionFactoryString + "\"");
ConnectionFactory connectionFactory = (ConnectionFactory) namingContext.lookup(connectionFactoryString);
log.info("Found connection factory \"" + connectionFactoryString + "\" in JNDI");
The exception occurs at this line:
ConnectionFactory connectionFactory = (ConnectionFactory) namingContext.lookup(connectionFactoryString);
These are the dependencies in pom.xml:
<dependency>
<groupId>org.jboss.eap</groupId>
<artifactId>wildfly-jms-client-bom</artifactId>
<version>7.3.4.GA</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.jboss.eap</groupId>
<artifactId>wildfly-ejb-client-bom</artifactId>
<version>7.3.4.GA</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.jms</groupId>
<artifactId>jboss-jms-api_2.0_spec</artifactId>
<version>1.0.0.Final-redhat-1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.jboss</groupId>
<artifactId>jboss-ejb-client</artifactId>
<version>4.0.37.Final</version>
</dependency>
<dependency>
<groupId>org.wildfly.common</groupId>
<artifactId>wildfly-common</artifactId>
<version>1.5.4.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.remotingjmx</groupId>
<artifactId>remoting-jmx</artifactId>
<version>3.0.4.Final</version>
</dependency>
It looks to me like your classpath has overlapping classes on it. I see the same thing in your pom.xml. This will cause the wrong classes to be loaded at runtime.
For example, if you look at the pom.xml of the HelloWorld-JMS project you'll see that it only uses the wildfly-jms-client-bom. In fact, the whole point of such a "bom" (i.e. bill of materials) is to incorporate all the required dependencies into a single dependency. Therefore, I recommend you just use this in your pom.xml:
<dependency>
<groupId>org.jboss.eap</groupId>
<artifactId>wildfly-jms-client-bom</artifactId>
<version>7.3.4.GA</version>
<type>pom</type>
</dependency>
Likewise, on your application's classpath you should only have the dependencies of wildfly-jms-client-bom on your classpath (or aggregated equivalents).
I am developping a multi-tier software using java. The middle tier is a java ejb accessing the postgresql database. But when I run the client java application, I have the following error messages:
INFO: EJBCLIENT000069: Using legacy jboss-ejb-client.properties security configuration
Exception in thread "main" javax.ejb.EJBException: WFLYEJB0442: Unexpected Error
....
Caused by: java.lang.NoClassDefFoundError: org/postgresql/Driver
...
Caused by: java.lang.ClassNotFoundException: org.postgresql.Driver
The content of the module.xml file is:
<?xml version="1.0" encoding="utf-8" ?>
<module xmlns="urn:jboss:module:1.3" name="org.postgresql">
<resources>
<!--the name of your driver -->
<resource-root path="postgresql-42.2.6.jar"/>
</resources>
<dependencies>
<module name="javax.api"/>
<module name="javax.transaction.api"/>
</dependencies>
</module>
The following lines have been added to the standalone.xml file:
<driver name="postgresql" module="org.postgresql">
<driver-class>org.postgresql.Driver</driver-class>
<xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-
class>
</driver>
The driver and the module.xml are in folder \modules\org\postgresql\main. I have successfully tested my code by moving it to a single-tier java application. I am using Eclipse jee latest verion, wildfly 16, postgresql 11. the driver is postgresql-42.2.6.jar
I have already spent several days battling with the problem, but no success. Help most welcome.
package loginPackage;
import java.sql.Connection;
import java.sql.DriverManager;
import org.postgresql.Driver;
import java.sql.SQLException;
public class LoginDao {
static String errorMessage;
static String connectionResult;
public static String getConnectionResult() {
return connectionResult;
}
public static void setConnectionResult(String connectResult) {
connectionResult = connectResult;
}
public String getErrorMessage() {
return errorMessage;
}
public static void setErrorMessage(String errorMsg) {
errorMessage = errorMsg;
}
public static void LoginCheck(String userCode, String userPswd) {
setConnectionResult(userConnect(userCode, userPswd));
}
public static String userConnect(String userCode, String userPaswd) {
try {
//Connection conn = null;
setConnectionResult("OK");
setErrorMessage("OK");
Driver driver = new org.postgresql.Driver();
DriverManager.registerDriver(driver);
Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/TestDb", userCode, userPaswd);
System.out.println("Connected to PostgreSQL database!");
return "SUCCESS";
}
catch (Exception e) {
System.out.println("Failed to create JDBC db connection " + e.toString() + e.getMessage());
setErrorMessage(userCode + ", " + userPaswd + ", " + e.toString() + ", " + e.getMessage() + ", " + e.getStackTrace());
return "FAILURE" ;
}
}
}
I am trying to set a new InitialContext in the following manner (which is pretty standard I believe):
private static InitialContext getInitialContext() throws NamingException {
InitialContext context = null;
try {
Properties properties = new Properties();
properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
properties.put(Context.PROVIDER_URL, "remote://localhost:4447");
properties.put(Context.SECURITY_PRINCIPAL, "username");
properties.put(Context.SECURITY_CREDENTIALS, "password");
context = new InitialContext(properties);
System.out.println("\n\tGot initial Context: " + context);
}
catch (Exception e) {
e.printStackTrace();
}
return context;
}
public static void sendMessage(RoboticsParameters object_msg) throws Exception {
InitialContext context = getInitialContext();
// other code
}
The code "fails" at the line where the new InitialContext is created using the properties and i get a java.lang.NullPointerException. I suspect I am missing an argument. Here is the stack trace:
WARN: EJB client integration will not be available due to a problem setting up the EJB client handler java.lang.NullPointerException
at org.jboss.naming.remote.client.InitialContextFactory.<clinit>(InitialContextFactory.java:118)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:72)
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:61)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:672)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
at javax.naming.InitialContext.init(InitialContext.java:244)
at javax.naming.InitialContext.<init>(InitialContext.java:216)
Any suggestions?
I am running JBoss EAP 6.4 and using EJB 3. I have jboss-client.jar in the class path.
I checked the source code for:
jboss-remote-naming/src/main/java/org/jboss/naming/remote/client/InitialContextFactory.java
and found where the log message was coming from:
public class InitialContextFactory implements javax.naming.spi.InitialContextFactory {
// code
private static final String REMOTE_NAMING_EJB_CLIENT_HANDLER_CLASS_NAME = "org.jboss.naming.remote.client.ejb.RemoteNamingStoreEJBClientHandler";
// code
try {
klass = classLoader.loadClass(REMOTE_NAMING_EJB_CLIENT_HANDLER_CLASS_NAME);
method = klass.getMethod("setupEJBClientContext", new Class<?>[] {Properties.class, List.class});
} catch (Throwable t) {
logger.warn("EJB client integration will not be available due to a problem setting up the EJB client handler", t);
}
// other code
}
The class org.jboss.naming.remote.client.ejb.RemoteNamingStoreEJBClientHandler was in the jar that I added to the class path but for some reason there were problems loading the class.
Then I stumbled upon this small README-EJB-JMS.txt file in the [jboss_home]/bin/client folder which states the following:
"Maven users should not use this jar, but should use the following BOM dependencies instead
<dependencies>
<dependency>
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-ejb-client-bom</artifactId>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-jms-client-bom</artifactId>
<type>pom</type>
</dependency>
</dependencies>
This is because using maven with a shaded jar has a very high chance of causing class version conflicts, which is why
we do not publish this jar to the maven repository."
So, I added the maven dependency instead of having the jar in my class path and VOILA! It works!
Is there a way to get a list of all deployments on a Jboss 6.0
implemented in Java either using JMX (ServerMBean) or JNDI?
On Wildfly 9.0.1.Final the code looks like this:
String host = "localhost";
int port = 9990;
String urlString = System.getProperty("jmx.service.url","service:jmx:http-remoting-jmx://" + host + ":" + port);
JMXServiceURL serviceURL = new JMXServiceURL(urlString);
JMXConnector jmxConnector = JMXConnectorFactory.connect(serviceURL, null);
MBeanServerConnection connection = jmxConnector.getMBeanServerConnection();
ObjectName name = new ObjectName("jboss.modules:type=ModuleLoader,name=*");
Set<ObjectInstance> objectInstances = connection.queryMBeans(name, null);
for (ObjectInstance objectInstance : objectInstances) {
if (objectInstance.getObjectName().getCanonicalName().contains("ServiceModuleLoader")) {
System.out.println("invoking method on " + objectInstance.getObjectName());
Object dumpAllModuleInformation = connection.invoke(objectInstance.getObjectName(), "dumpAllModuleInformation", new Object[]{}, new String[]{});
System.out.println(dumpAllModuleInformation);
}
}
jmxConnector.close();
Maybe you can adapt this to JBoss 6. On Wildfly 9.0.1.Finale this outputs:
Module deployment.stackoverflow-jboss-1.0-SNAPSHOT.war:main
Class loader: ModuleClassLoader for Module "deployment.stackoverflow-jboss-1.0-SNAPSHOT.war:main" from Service Module Loader
Resource Loaders:
Loader Type: org.jboss.as.server.deployment.module.VFSResourceLoader
Paths:
META-INF
META-INF/maven
META-INF/maven/stackoverflow-jboss