How do I add aliases to a Servlet Context in java? - servlets

I have a servlet running under Tomcat.
I need to serve some files, I guess we can call them semi-static (which change occasionally ... they are updated by another part of the app) from an external (to the WEB-APP) directory.
I have managed to do this by adding the following to my context.xml in the META-INF directory
<Context aliases="/working_dir=c:/apache_tomcat_working_dir" ></Context>
This works fine, in my HTML I refer to the file as
<img src="/myWebbApp/working_dir/fixpermin_zoom.png">
and in my web.xml inside WEB-INF
I let the default server handle png files as follows
<!-- use default for static serving of png's, js and css, also ico -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
So this works fine. But I want to set the external directory from inside java code, not by editing the context.xml file.
Now in the init() method of the servlet I can get the ServletContext.
ServletContext sc = getServletContext();
If I examine this variable sc in the debugger, I can see the alias string several levels deep, see the attached image. How can I get at this alias string programatically?
I have checked the ServletContext docs, but i can't find it very helpful.
Any help much appreciated.
(source: choicecomp.com)

As you can see in your debugger, your context is Tomcat's Context Object org.apache.catalina.core.StandardContext
You can try following steps in Tomcat 6 and below:
StandardEngine engine = (StandardEngine) ServerFactory.getServer().findService("Catalina").getContainer();
StandardContext context = (StandardContext) engine.findChild(engine.getDefaultHost()).findChild(getServletContext().getContextPath());
Mapper mapper = context.getMapper();
Now you can add Host alias using addHostAlias(String HostName, String alias) method of the Mapper class.
mapper.addHostAlias(engine.getDefaultHost(), "myAlias");
Here is the code snippet for Tomcat 7:
MBeanServer mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0);
ObjectName name = new ObjectName("Catalina", "type", "Server");
Server server = (Server) mBeanServer.getAttribute(name, "managedResource");
StandardEngine engine = (StandardEngine) server.findService("Catalina").getContainer();
StandardContext context = (StandardContext) engine.findChild(engine.getDefaultHost()).findChild(getServletContext().getContextPath());
Mapper mapper = context.getMapper();
mapper.addHostAlias(engine.getDefaultHost(), "myAlias");
If there is no host in the mapper, please try below:
MBeanServer mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0);
ObjectName name = new ObjectName("Catalina", "type", "Server");
Server server = (Server) mBeanServer.getAttribute(name, "managedResource");
StandardEngine engine = (StandardEngine) server.findService("Catalina").getContainer();
StandardContext context = (StandardContext) engine.findChild(engine.getDefaultHost()).findChild(getServletContext().getContextPath());
Mapper mapper = context.getMapper();
//just a clean up step(remove the host)
mapper.removeHost(engine.getDefaultHost());
//add the host back with all required aliases
mapper.addHost(engine.getDefaultHost(), new String[]{"myAlias"}, engine.getDefaultHost());
Hope this helps!

I found another method StandardContext.setAliases. Find below the full working code snippet for Tomcat 7.0.30.
MBeanServer mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0);
ObjectName name = new ObjectName("Catalina", "type", "Server");
Server server = (Server) mBeanServer.getAttribute(name, "managedResource");
StandardEngine engine = (StandardEngine) server.findService("Catalina").getContainer();
StandardContext context = (StandardContext) engine.findChild(engine.getDefaultHost()).findChild(getServletContext().getContextPath());
context.setAliases("myAlias");
//infact aliases should be proper e.g. below
//context.setAliases("/aliasPath1=docBase1,/aliasPath2=docBase2");
Mapper mapper = context.getMapper();
mapper.removeHost(engine.getDefaultHost());
mapper.addHost(engine.getDefaultHost(), new String[]{"myAlias"}, engine.getDefaultHost());
mapper.addHostAlias(engine.getDefaultHost(), "myAlias");
//infact aliases should be proper e.g. below
//mapper.addHostAlias(engine.getDefaultHost(), "/aliasPath1=docBase1,/aliasPath2=docBase2");
Please find my debugger screenshots below:
Before the code snippet execution:
After the code snippet execution:
Hope this is more helpful.

Here is my working code to dynamically set Tomcat7 context alias depending on different operating systems. Sure you can improve on it
public class ContextListener implements ServletContextListener {
#Override
public void contextInitialized(ServletContextEvent sce) {
ServletContext context = sce.getServletContext();
// tomcat 7.x
try {
MBeanServer mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0);
ObjectName name = new ObjectName("Catalina", "type", "Server");
Object server = mBeanServer.getAttribute(name, "managedResource");
Object service = server.getClass().getMethod("findService", String.class).invoke(server, "Catalina"); //StandardService[Catalina]
Object connectors = service.getClass().getMethod("findConnectors").invoke(service);
Object engine = service.getClass().getMethod("getContainer").invoke(service); //StandardEngine[Catalina]
Object host = Array.get(engine.getClass().getMethod("findChildren").invoke(engine), 0); //StandardHost[Catalina]
Object stdContext = Array.get(host.getClass().getMethod("findChildren").invoke(host), 0); //StandardContext[Catalina]
Object mapper = stdContext.getClass().getMethod("getMapper").invoke(stdContext);
//just a clean up step(remove the host)
Field f1 = mapper.getClass().getDeclaredField("context");
f1.setAccessible(true);
Object ct = f1.get(mapper);
Field f2 = ct.getClass().getDeclaredField("resources");
f2.setAccessible(true);
Object rs = f2.get(ct);
Field f3 = rs.getClass().getDeclaredField("dirContext");
f3.setAccessible(true);
Object dc = f3.get(rs);
mapper.getClass().getMethod("removeHost",String.class).invoke(mapper, host.getClass().getMethod("getName").invoke(host));
//add the host back with all required aliases
switch (OsCheck.getOperatingSystemType()) {
case Windows:
dc.getClass().getMethod("setAliases",String.class).invoke(dc,"/img/avatars=" + winAvatarAlias);
break;
default:
dc.getClass().getMethod("setAliases",String.class).invoke(dc,"/img/avatars=" + linuxAvatarAlias);
break;
}
String ports = "";
for (Object o :(Object[]) connectors ) {
ports = ports + (Integer)o.getClass().getMethod("getPort").invoke(o) + " ";
}
log.info("Tomcat 7.x detected, service {}, engine {}, host {}, stdContext {}, server port: {}",
service.getClass().getMethod("getName").invoke(service),
engine.getClass().getMethod("getName").invoke(engine),
host.getClass().getMethod("getName").invoke(host),
stdContext.getClass().getMethod("getDisplayName").invoke(stdContext),
ports);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Based on Khanh's approach, here is a context listener that works for an embedded maven tomcat (v.7.0.62).
Please note the differences ("Tomcat" instead of "Catalina" and no findService("Catalina")), so that the approach works for an embedded tomcat. In contrast to Khanh, I used regular methods instead of reflection to get the BaseDirContext object.
Finally, you should note that you need to call setAliases() on the BaseDirContext object instead of the StandardContext object! Internally, StandardContext's setAliases() is just a setter, whereas BaseDirContext's setAliases() does a lot of other stuff, so that the already running tomcat indeed registers your new aliases.
import org.apache.catalina.Container;
import org.apache.catalina.Server;
import org.apache.catalina.Service;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.core.StandardEngine;
import org.apache.log4j.Logger;
import org.apache.naming.resources.BaseDirContext;
import org.apache.naming.resources.ProxyDirContext;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class AliasesContextListener implements ServletContextListener {
private static Logger log = Logger.getLogger(AliasesContextListener.class);
#Override
public void contextInitialized(ServletContextEvent sce) {
try {
String aliases = "/foo=C:\\bar";
//get current tomcat server, engine and context objects
MBeanServer mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0);
ObjectName name = new ObjectName("Tomcat", "type", "Server");
Server server = (Server) mBeanServer.getAttribute(name, "managedResource");
Service[] services = server.findServices();
StandardEngine engine = (StandardEngine) services[0].getContainer();
Container defaultHostContainer = engine.findChild(engine.getDefaultHost());
ServletContext servletContext = sce.getServletContext();
StandardContext standardContext = (StandardContext) defaultHostContainer.findChild(servletContext.getContextPath());
ProxyDirContext proxyDirContext = (ProxyDirContext) standardContext.getResources();
BaseDirContext baseDirContext = (BaseDirContext) proxyDirContext.getDirContext();
//modify the aliases entry
baseDirContext.setAliases(aliases);
} catch (Exception e) {
log.error("error while setting aliases in context listener", e);
}
}
#Override
public void contextDestroyed(ServletContextEvent sce) {
//not implemented
}
}

Related

HornetQ JMS: java.lang.NoSuchMethodError

unfortunately while trying to get a JMS service working with HornetQ, I am running form error message to error message. The prior error was related to a missing core.jar file. Prior Error
After all, I am trying to implement a simple producer/consumer example as a jms service. (Based on "HornetQ Messaging Developer's Guide")
package chapter01;
import javax.jms.JMSException;
import javax.naming.NamingException;
public class ECGMessageConsumerProducerExample {
public static void main(String[] args) throws NamingException, JMSException {
// TODO Auto-generated method stub
javax.naming.Context ic = null;
javax.jms.ConnectionFactory cf = null;
javax.jms.Connection connection = null;
javax.jms.Queue queue = null;
javax.jms.Session session = null;
com.mongodb.Mongo m;
com.mongodb.DB db;
String destinationName = "queue/DLQ";
java.util.Properties p = new java.util.Properties();
p.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
p.put(javax.naming.Context.URL_PKG_PREFIXES,
"org.jboss.naming:org.jnp.interfaces");
p.put(javax.naming.Context.PROVIDER_URL, "jnp://localhost:1099");
ic = new javax.naming.InitialContext(p);
cf = (javax.jms.ConnectionFactory)ic.lookup("/ConnectionFactory");
queue = (javax.jms.Queue)ic.lookup(destinationName);
connection = cf.createConnection();
session = connection.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
connection.start();
String theECG = "1;02/20/2012 14:01:59.010;1020,1021,1022";
javax.jms.MessageProducer publisher = session.createProducer(queue);
javax.jms.TextMessage message =
session.createTextMessage(theECG);
publisher.send(message);
System.out.println("Message sent!");
publisher.close();
}
}
While facing erros I probably added unneeded jars:
However, I get the following error message:
Exception in thread "main" java.lang.NoSuchMethodError: org.hornetq.api.core.client.ClientSessionFactory.copy()Lorg/hornetq/api/core/client/ClientSessionFactory;
at org.hornetq.jms.client.HornetQConnectionFactory.createConnectionInternal(HornetQConnectionFactory.java:612)
at org.hornetq.jms.client.HornetQConnectionFactory.createConnection(HornetQConnectionFactory.java:116)
at org.hornetq.jms.client.HornetQConnectionFactory.createConnection(HornetQConnectionFactory.java:111)
at chapter01.ECGMessageConsumerProducerExample.main(ECGMessageConsumerProducerExample.java:33)
I expect the error to be caused by a mismatch between jars used by the server and used by the client. But since I am a programmer noob, I do not really know what exact jars with suitable versions are needed.
Try defining NettyConnectorFactory as connector as in jboss reference.
for example in hornetq-configuration.xml file:
<connectors>
<connector name="netty">
<factory-class>
org.hornetq.core.remoting.impl.netty.NettyConnectorFactory
</factory-class>
<param key="port" value="5446"/>
</connector>
</connectors>

How to reload apache commons configurations2 properties

can anyone guide me on how to perform a reload of an apache commons configuration2 properties. I'm unable to find any implementation of this anywhere. The apache docs are a bit too abstract. This is what I have so far but it's not working.
CombinedConfiguration cc = new CombinedConfiguration();
Parameters params = new Parameters();
File configFile = new File("config.properties");
File emsFile = new File("anotherconfig.properties");
ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration> configBuilder =
new ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration>(PropertiesConfiguration.class)
.configure(params.fileBased()
.setFile(configFile));
PeriodicReloadingTrigger reloadTrg = new PeriodicReloadingTrigger(configBuilder.getReloadingController(), null, 5, TimeUnit.SECONDS);
reloadTrg.start();
cc.addConfiguration(configBuilder.getConfiguration());
FileBasedConfigurationBuilder<FileBasedConfiguration> emsBuilder =
new FileBasedConfigurationBuilder<FileBasedConfiguration>(PropertiesConfiguration.class)
.configure(params.properties()
.setFile(emsFile));
cc.addConfiguration(emsBuilder.getConfiguration());
DataSource ds = EmsDataSource.getInstance().getDatasource(this);
BasicConfigurationBuilder<DatabaseConfiguration> dbBuilder =
new BasicConfigurationBuilder<DatabaseConfiguration>(DatabaseConfiguration.class);
dbBuilder.configure(
params.database()
.setDataSource(ds)
.setTable("EMS_CONFIG")
.setKeyColumn("KEY")
.setValueColumn("VALUE")
);
cc.addConfiguration(dbBuilder.getConfiguration());
The configuration obtained from a builder is not updated automatically. You need to get the configuration from the builder every time you read it.
From Automatic Reloading of Configuration Sources:
One important point to keep in mind when using this approach to reloading is that reloads are only functional if the builder is used as central component for accessing configuration data. The configuration instance obtained from the builder will not change automagically! So if an application fetches a configuration object from the builder at startup and then uses it throughout its life time, changes on the external configuration file become never visible. The correct approach is to keep a reference to the builder centrally and obtain the configuration from there every time configuration data is needed.
use following code:
#Component
public class ApplicationProperties {
private PropertiesConfiguration configuration;
#PostConstruct
private void init() {
try {
String filePath = PropertiesConstants.PROPERTIES_FILE_PATH;
System.out.println("Loading the properties file: " + filePath);
configuration = new PropertiesConfiguration(filePath);
//Create new FileChangedReloadingStrategy to reload the properties file based on the given time interval
FileChangedReloadingStrategy fileChangedReloadingStrategy = new FileChangedReloadingStrategy();
fileChangedReloadingStrategy.setRefreshDelay(PropertiesConstants.REFRESH_DELAY);
configuration.setReloadingStrategy(fileChangedReloadingStrategy);
} catch (ConfigurationException e) {
e.printStackTrace();
}
}
public String getProperty(String key) {
return (String) configuration.getProperty(key);
}
public void setProperty(String key, Object value) {
configuration.setProperty(key, value);
}
public void save() {
try {
configuration.save();
} catch (ConfigurationException e) {
e.printStackTrace();
}
}
}

NoInitialContextException in simple EJB Project on Wildfly 8

I am trying to write a client for a simple EJB Project that I have deployed locally on my Wildfly 8 running in Eclipse.
My Interface:
package com.jwt.ejb.business;
import javax.ejb.Remote;
#Remote
public interface Hello {
public String sayHello();
}
My Implementation:
package com.jwt.ejb.businesslogic;
import javax.ejb.Stateless;
import com.jwt.ejb.business.Hello;
#Stateless
public class HelloBean implements Hello {
public HelloBean() {
}
public String sayHello() {
return "Hello Boss Welcome to EJB";
}
}
My client:
package com.jwt.ejb.test;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.jwt.ejb.business.Hello;
import com.jwt.ejb.businesslogic.HelloBean;
public class Client {
public static void main(String[] args) {
Hello bean = doLookup();
if (bean != null)
System.out.println(bean.sayHello());
}
private static Hello doLookup() {
Context context = null;
Hello bean = null;
try {
final Hashtable jndiProperties = new Hashtable();
jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
context = new InitialContext(jndiProperties);
bean = (Hello) context.lookup(getLookupString());
} catch (NamingException e) {
e.printStackTrace();
}
return bean;
}
private static String getLookupString() throws NamingException {
final String appName = "";
final String moduleName = "EJBTest";
final String distinctName = "";
final String beanName = HelloBean.class.getSimpleName();
final String viewClassName = Hello.class.getName();
return "ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName;
}
}
When I run it, I get this Exception:
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at com.jwt.ejb.test.Client.doLookup(Client.java:26)
at com.jwt.ejb.test.Client.main(Client.java:15)
Exception in thread "main" java.lang.NullPointerException
at com.jwt.ejb.test.Client.main(Client.java:16)
The official documentation does it like this too:
https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+client+using+JNDI
Where is the problem ?
I came accross posts talking about boss-ejb-client.properties, but I am not sure what to put in it. As I understood it, I could either have this properties file, or declare the porperties programmatically, like I did.
Besides the fact that you are using Wildfly 8 and referencing JBoss AS 7.1's documentation, you're missing the InitialContextFactory on your JNDI properties.
Wildfly's documentation on EJB invocations via JNDI is here. Therefore you should do the following:
void doBeanLookup() {
Properties jndiProperties = new Properties();
jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
jndiProperties.put(Context.PROVIDER_URL, "http-remoting://localhost:8080");
// This is an important property to set if you want to do EJB invocations via the remote-naming project
jndiProps.put("jboss.naming.client.ejb.context", true);
// create a context passing these properties
Context ctx = new InitialContext(jndiProps);
// lookup the bean Hello
Hello bean = (Hello) ctx.lookup(getLookupString());
}
In your pom.xml you should have the following:
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-ejb-client-bom</artifactId>
<version>8.0.0.Final</version>
<type>pom</type>
</dependency>
If you want to use the http-remoting protocol, on your getLookupString method, you should remove the ejb: and have present that
http-remoting client assumes JNDI names in remote lookups are relative to java:jboss/exported namespace, a lookup of an absolute JNDI name will fail.
Further information can be found on Wildfly 8 Remote JNDI Reference Update Draft.

Weblogic 10.3.5 EJB3 local seesion bean lookup from java main class

I am using Weblogic 10.3.5 and EJB 3 for session bean
but I am not able to lookup jndi for local stateless session bean even though I am able to lookup
remote bean successfully
my code are for main class is
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
p.put(Context.PROVIDER_URL, "t3://localhost:7001");
try {
Context ctx = new InitialContext(p);
TheBeanRemote bean = (TheBeanRemote) ctx
.lookup("MrBean#com.bdc.TheBeanRemote");
System.out.println(bean.sayHello());
TheLocalLocal bean2 = (TheLocalLocal) ctx.lookup("TheLocalLocal");
Object obj = ctx.lookup("java:comp/env/ejb/MrBean2");
System.out.println(bean2.sayHello());
} catch (Exception e) {
e.printStackTrace();
}
Remote Bean
import javax.ejb.Remote;
#Remote
public interface TheBeanRemote {
public String sayHello();
}
Local Bean
import javax.ejb.Local;
#Local(TheLocalLocal.class)
public interface TheLocalLocal {
public String sayHello();
}
If you're running as a "client", that is a remote call, if that app is running on the server itself, that is a local call. It makes sense that one works but not the other.
The properties you're using indicate a remote call:
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
p.put(Context.PROVIDER_URL, "t3://localhost:7001");
Context ctx = new InitialContext(p);
A local call is:
Context context = new InitialContext();
See a similar question here: How to lookup JNDI resources on WebLogic?

JBoss 7.1.1.Final - EJB Remote Call - java.lang.IllegalStateException: No EJB receiver available for handling

I do have 2 JBoss stanalon instance running. 1 act as Server and another 1 would client.
SERVER:
Remote Interface
package com.xyz.life.service.ejb;
import java.io.Serializable;
import java.rmi.RemoteException;
import javax.ejb.EJB;
import javax.ejb.Remote;
#Remote
public interface QuoteFacade extends Serializable{
public boolean isAlive() throws RemoteException;
}
EJB Impl
package com.xyz.life.common.component.ejb.services;
import java.rmi.RemoteException;
import javax.ejb.Remote;
import javax.ejb.Stateless;
#Stateless(mappedName = "QuoteFacadeEJB")
#Remote(QuoteFacade.class)
public class QuoteFacadeEJB extends CommonSessionBean implements QuoteFacade {
private static final long serialVersionUID = -8788783322280644881L;
#Override
public boolean isAlive() throws RemoteException {
return true;
}
}
server.log
16:40:25,012 INFO [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-4) JNDI bindings for session bean named QuoteFacadeEJB in deployment unit subdeployment "quote.jar" of deployment "quote.ear" are as follows:
java:global/quote/quote.jar/QuoteFacadeEJB!com.xyz.life.service.ejb.QuoteFacade
java:app/quote.jar/QuoteFacadeEJB!com.xyz.life.service.ejb.QuoteFacade
java:module/QuoteFacadeEJB!com.xyz.life.service.ejb.QuoteFacade
java:jboss/exported/quote/quote.jar/QuoteFacadeEJB!com.xyz.life.service.ejb.QuoteFacade
java:global/quote/quote.jar/QuoteFacadeEJB
java:app/quote.jar/QuoteFacadeEJB
java:module/QuoteFacadeEJB
Client
public void testClient() {
try {
Hashtable<String, Object> jndiProps = new Hashtable<String, Object>();
jndiProps.put(Context.URL_PKG_PREFIXES, JNDINames.JBOSS_CLIENT_NAMING_PREFIX);
jndiProps.put("jboss.naming.client.ejb.context", true);
Context ctx = new InitialContext(jndiProps);
String name = "ejb:global/quote/quote.jar/QuoteFacadeEJB!com.ge.life.annuity.service.ejb.QuoteFacade";
/*
"ejb:global/quote/quote.jar/QuoteFacadeEJB!com.ge.life.annuity.service.ejb.QuoteFacade",
"ejb:app/quote.jar/QuoteFacadeEJB!com.ge.life.annuity.service.ejb.QuoteFacade",
"ejb:jboss/exported/quote/quote.jar/QuoteFacadeEJB!com.ge.life.annuity.service.ejb.QuoteFacade"
*/
Object ref = ctx.lookup(name);
QuoteFacade quoteFacade = (QuoteFacade) ref;
LOGGER.debug("isAlive : " + quoteFacade.isAlive());
} catch (Exception e) {
LOGGER.error("Remote Client Exception : ", e);
}
}
No error/log on server side. Client side, it is failing with following error:
java.lang.IllegalStateException: No EJB receiver available for handling [appName:global,modulename:quote,distinctname:quote.jar] combination for invocation context org.jboss.ejb.client.EJBClientInvocationContext#200cae
at org.jboss.ejb.client.EJBClientContext.requireEJBReceiver(EJBClientContext.java:584)
at org.jboss.ejb.client.ReceiverInterceptor.handleInvocation(ReceiverInterceptor.java:119)
at org.jboss.ejb.client.EJBClientInvocationContext.sendRequest(EJBClientInvocationContext.java:181)
at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:136)
at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:121)
at org.jboss.ejb.client.EJBInvocationHandler.invoke(EJBInvocationHandler.java:104)
at $Proxy10.isAlive(Unknown Source)
I tried without using Properties file:
private static QuoteFacade connectToStatelessBean(String name) throws NamingException {
Properties jndiProperties = new Properties();
jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
jndiProperties.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
jndiProperties.put(javax.naming.Context.PROVIDER_URL, "remote://localhost:4447");
jndiProperties.put(javax.naming.Context.SECURITY_PRINCIPAL, "admin");
jndiProperties.put(javax.naming.Context.SECURITY_CREDENTIALS, "Pass1234");
final Context context = new InitialContext(jndiProperties);
return (QuoteFacade) context.lookup(name);
}
public static void testLocal() {
String[] JNDINAME1 = {
"ejb:global/quote/quote.jar/QuoteFacadeEJB!com.ge.life.annuity.service.ejb.QuoteFacade",
"ejb:app/quote.jar/QuoteFacadeEJB!com.ge.life.annuity.service.ejb.QuoteFacade",
"ejb:module/QuoteFacadeEJB!com.ge.life.annuity.service.ejb.QuoteFacade",
"ejb:jboss/exported/quote/quote.jar/QuoteFacadeEJB!com.ge.life.annuity.service.ejb.QuoteFacade",
"ejb:global/quote/quote.jar/QuoteFacadeEJB",
"ejb:app/quote.jar/QuoteFacadeEJB",
"ejb:module/QuoteFacadeEJB"
};
for(int i=0;i<JNDINAME1.length;i++){
try {
QuoteFacade test1 = connectToStatelessBean(JNDINAME1[i]);
LOGGER.error("DSLKAJDLAS : " + test1.isAlive());
} catch (Exception e) {
LOGGER.error("DSLKAJDLAS : " , e);
}
}
LOGGER.info("Done - SANSSAN!!!!!!!!");
}
This time, different exception :
14.01.2013 17:40:37.627 [ERROR] - EJBClient - DSLKAJDLAS :
javax.naming.NamingException: JBAS011843: Failed instantiate InitialContextFactory org.jboss.naming.remote.client.InitialContextFactory from classloader ModuleClassLoader for Module "deployment.quote.war:main" from Service Module Loader
at org.jboss.as.naming.InitialContextFactoryBuilder.createInitialContextFactory(InitialContextFactoryBuilder.java:64)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:681)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
at javax.naming.InitialContext.init(InitialContext.java:242)
at javax.naming.InitialContext.><init>(InitialContext.java:216)
at com.xyz.life.test.EJBClient.connectToStatelessBean(EJBClient.java:208)
at com.xyz.life.test.EJBClient.testLocal(EJBClient.java:225)
at com.xyz.life.test.EJBClient.test(EJBClient.java:172)
at com.xyz.life.common.web.struts.plugin.FrameworkStartupPlugIn.init(FrameworkStartupPlugIn.java:99)
at org.apache.struts.action.ActionServlet.initModulePlugIns(ActionServlet.java:1158)
at org.apache.struts.action.ActionServlet.init(ActionServlet.java:473)
at javax.servlet.GenericServlet.init(GenericServlet.java:242)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1202)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1102)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3655)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:3873)
at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:90)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
Try removing "global" from name:
String name =
"ejb:quote/quote.jar/QuoteFacadeEJB!com.xyz.life.service.ejb.QuoteFacade"
Also, your package name should be com.xyz.life.service.ejb (as seen on server log) and not com.ge.life.annuity.service.ejb.
Anyway, using remote-naming project for remote EJB invocations is discouraged as explained here.
... . So as you can see, we have managed to optimize certain operations by using the EJB client API for EJB lookup/invocation as against using the remote-naming project. There are other EJB client API implementation details (and probably more might be added) which are superior when it is used for remote EJB invocations in client applications as against remote-naming project which doesn't have the intelligence to carry out such optimizations for EJB invocations. That's why the remote-naming project for remote EJB invocations is considered "deprecated". ...
You can check how to do remote EJB invocations using the EJB client API here.
Found it....
The ones I used for Local machines only. Difference JBoss instances, should change the JNDI lookup name...
like
ejb:quote/quote.jar//QuoteFacadeEJB!com.xyz.life.service.ejb.QuoteFacade

Resources