Java8/JBoss7 - Can't get instance of bean - java

I'm upgrading an app from JBoss 5 to 7 and I've been running into an issue where it can't find the bean which has been called.
Which of the JNDI bindings JBoss is actually looking for?
server.log (on startup)
WFLYEJB0473: JNDI bindings for session bean named 'ManagerLocal' in deployment unit 'subdeployment "Application-EJB.jar" of deployment "Application-EAR.ear"' are as follows:
java:global/Application-EAR/Application-EJB/ManagerLocal!com.application.data.manager.local.ManagerLocal
java:app/Application-EJB/ManagerLocal!com.application.data.manager.local.ManagerLocal
java:module/ManagerLocal!com.application.data.manager.local.ManagerLocal
java:global/Application-EAR/Application-EJB/ManagerLocal
java:app/Application-EJB/ManagerLocal
java:module/ManagerLocal
Error
javax.naming.NameNotFoundException: ManagerLocal -- service jboss.naming.context.java.ManagerLocal
at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:106)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:207)
at org.jboss.as.naming.InitialContext$DefaultInitialContext.lookup(InitialContext.java:235)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:193)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:189)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at com.common.util.LookupUtil.lookup(LookupUtil.java:91)
at com.common.util.LookupUtil.lookupLocalBean(LookupUtil.java:35)
at com.application.web.resource.RequestResource.getRequestManager(RequestResource.java:69)
at com.application.web.resource.RequestResource.searchRequests(RequestResource.java:413)
at com.application.trigger.handler.TriggerHandler.execute(TriggerHandler.java:43)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
2018-10-05 16:25:00,030 [ApplicationScheduler_Worker-3] ERROR org.quartz.core.JobRunShell - Job AppJobs.AppBuild threw an unhandled Exception:
java.lang.RuntimeException: Couldn't get an instance of manager bean
Code to compute JNDI name
private static String computeJNDIName(boolean isLocal, String beanName, String appName) {
String name;
if (isLocal) {
name = appName + "/" + beanName + "/local";
} else {
name = appName + "/" + beanName + "/remote";
}
return name;
}

Not the greatest answer, e.g. it doesnt state the why or reasoning, but I had to add this to a JSF backing bean in my WAR so it could access a bean in the EJB jar when I upgraded to EAP7
#EJB(lookup="java:app/NEDtoBrsMigratorEJB/CustomerDeleterBean")
CustomerDeleterBeanLocal customerDeleterBean;
The JNDI binding for the EJB is:
java:global/NEDtoBrsMigratorEAR/NEDtoBrsMigratorEJB/CustomerDeleterBean!net.myco.brsupdater.engine.CustomerDeleterBeanLocal
java:app/NEDtoBrsMigratorEJB/CustomerDeleterBean!net.myco.brsupdater.engine.CustomerDeleterBeanLocal
java:module/CustomerDeleterBean!net.myco.brsupdater.engine.CustomerDeleterBeanLocal
java:global/NEDtoBrsMigratorEAR/NEDtoBrsMigratorEJB/CustomerDeleterBean
java:app/NEDtoBrsMigratorEJB/CustomerDeleterBean
java:module/CustomerDeleterBean
And note, that example annotation is for in VM JNDI lookup.

Related

HstSiteConfigurer: ComponentManager initialization failed

I'm working on One Hippo CMS, when I run the Tomcat server I'm getting this error
5.02.2019 18:23:45 ERROR main [DefaultHstSiteConfigurer.initializeComponentManager:264] HstSiteConfigurer: ComponentManager initialization failed.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.hippoecm.hst.content.beans.manager.ObjectConverter' defined in class path resource [org/hippoecm/hst/site/container/SpringComponentManager-content-beans.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Annotated class 'org.example.myproject.beans.Urldemo' for primarytype 'myproject:urldemo' is a duplicate of already registered class 'org.example.myproject.beans.Simple'. You might have configured a bean that does not have a annotation for the jcrType and inherits the jcrType from the bean it extends, resulting in 2 beans with the same jcrType. Correct your beans.
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1745) ~[spring-beans-5.1.1.RELEASE.jar:5.1.1.RELEASE]
Check the annotations on:
org.example.myproject.beans.Urldemo
org.example.myproject.beans.Simple
They should each have an annotation similar to:
#Node(jcrType = "project:doctype")
Each needs a unique jcrType value.

Jboss AS 7.1.1 Final Local JNDI EJB Invocation

Using Jboss AS 7.1.1 Final, I'm trying to invoke a Local EJB(3.1) through JNDI.
My Local EJB is:
#Stateless(mappedName = "Services")
#LocalBean
public class Services implements ServicesLocal {
.....// scary stuffs here
}
My Interface Services is:
#Local
public interface ServicesLocal {
.... // Some powerfull stuffs here
}
I'm trying to invoke this EJB above like this:
private ServicesLocal getLocalEJB() throws NamingException {
log.info("\n\n\n\n\n\n\n\n ################## Getting the ServicesLocal");
InitialContext context = new InitialContext();
return (ServicesLocal) context.lookup("ejb:/global/docs/docs-ejb-0.1/Services!com.mycompany.docs.local.ServicesLocal");
}
This is the error I'm getting when invoking this code above:
############ Getting the ServicesLocal
15:36:10,437 INFO [org.jboss.ejb.client] (http-localhost-127.0.0.1-8080-1) JBoss EJB Client version 1.0.5.Final
15:36:10,456
ERROR [stderr] (http-localhost-127.0.0.1-8080-1) java.lang.IllegalStateException: No EJB receiver available for handling [appName:,modulename:global,distinctname:docs] combination for invocation context org.jboss.ejb.client.EJBClientInvocationContext#396f6a24
15:36:10,457
ERROR [stderr] (http-localhost-127.0.0.1-8080-1) ^Iat org.jboss.ejb.client.EJBClientContext.requireEJBReceiver(EJBClientContext.java:584)
And this is the JNDI log when the Jboss 7.1.1 Final boot. Having the EJB that I want to invoke:
15:07:07,975 INFO [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-7) JNDI bindings for session bean named Services in deployment unit subdeployment "docs-ejb-0.1.jar" of deployment "docs.ear" are as follows:
java:global/docs/docs-ejb-0.1/Services!com.mycompany.docs.local.ServicesLocal
java:app/docs-ejb-0.1/Services!com.mycompany.docs.local.ServicesLocal
java:module/Services!com.mycompany.docs.local.ServicesLocal
java:global/docs/docs-ejb-0.1/Services!com.mycompany.docs.services.Services
java:app/docs-ejb-0.1/Services!com.mycompany.docs.services.Services
java:module/Services!com.mycompany.docs.services.Services
I think my context.lookup("ejb:/global/docs/docs-ejb-0.1/Services!com.mycompany.docs.local.ServicesLocal") is wrong. What I can do to fix that and invoke my Local EJB with JNDI?
Have you tried to use what JBoss tells you?
context.lookup("java:global/docs/docs-ejb-0.1/Services!com.mycompany.docs.local.ServicesLocal")
Your EJB is stored in java namespace, not in ejb one.

Remote lookup to another Glassfish from a Servlet

I'm trying to run a remote lookup to another Glassfish from a Servlet. So, I was following the link documentation (http://docs.oracle.com/cd/E19798-01/821-1752/beanv/index.html). First I created a Sateless Session Ben called CalculatorBean, packaged in an EJB JAR of the same name (CalculatorBean), the JNDI name was java:global/CalculatorBean/CalculatorBean.
According to the documentation, I created a Web project and declared my EJB in sub-web.xml the following file:
<ejb-ref>
<ejb-ref-name>ejb/CalculatorBean</ejb-ref-name>
<jndi-name>corbaname:iiop:127.0.0.1:3700#CalculatorBean/CalculatorBean</jndi-name>
</ejb-ref>
where 127.0.0.1 is the host of the machine (local!), 3700 is the default port for querying and, CalculatorBean/CalculatorBean is the global JNDI name. First question, theoretically the JNDI name passes into an interoperable String "CalculatorBean/CalculatorBean" instead of "java: global/CalculatorBean/CalculatorBean", right?
After that, I created a Servlet and put the following code snippet:
ctx = new InitialContext ();
bean = (CalculatorRemote) ctx.lookup ("java:comp/env/ejb/CalculatorBean");
Where, CalculatorRemote is the name of the remote interface that we included in the java project:comp/env/ is the directory section to access Java EE components and ejb/CalculatorBean is the name of my bean in the configuration of the sun-web.xml file
 
When put to run my Servlet I'm getting the exception:
Caused by: javax.naming.NameNotFoundException: No object bound to name java:comp/env/ejb/CalculatorBean
Obviously, it's not finding the name, however, do not really know what name I should use to set the lookup.
I had the same problem, and I solved it.
By default, your EJB is not visilbe into java:comp/env/ and you can not lookup for an EJB into InitialContext instance. But, you can successful lookup for an EJB after when at least one EJB instance is injected using #EJB annotation, like, for your example:
#EJB(name = "ejb/CalculatorBean")
private CalculatorRemote calc;
After that, CalculatorRemote EJB is visible in InitialContext instance.

Running Spring Security and Struts JUnit tests in tandem

Currently, in my web application, I'm trying to run a series of unit tests for Struts 2.0.6 and Spring 3. In doing so, I've come across an issue where I have both tests call on the context configuration paths separately in their own respective tests and throw exceptions saying that certain beans have already been wired.
For my Struts unit test, I've extended the BaseStrutsTestCase, found here:
http://depressedprogrammer.wordpress.com/2007/06/18/unit-testing-struts-2-actions-spring-junit
I've configured the context configuration path as follows for the struts action test:
private static final String CONFIG_LOCATIONS = "classpath:applicationContext-test.xml";
Then I've gone ahead and created a separate Spring JUnit test for the login functionality for Spring Security:
#RunWith(SpringJUnit4ClassRunner.class)
#WebAppConfiguration
#ContextConfiguration({"classpath:applicationContext-test.xml"})
public class UserAuthenticationTest {
#Autowired
protected UserManagementService userManagementService;
#Test
public void testUserSuccess() {
ArrayList<GrantedAuthority> grantedAuthorities =
new ArrayList<GrantedAuthority>();
ConsoleUser consoleUser =
userManagementService.getConsoleUser("user#test.com","password");
if (consoleUser == null){
Assert.assertTrue(consoleUser == null);
} else {
for (ConsoleRole role : consoleUser.getRoles()){
StringBuilder permissions = new StringBuilder();
for (ConsolePermission permission : role.getPermissions()){
grantedAuthorities.add(new GrantedAuthorityImpl(permission.getPermissionType().toString()));
permissions.append(permission.getPermissionType().toString() + " ");
}
}
Authentication authentication = new UsernamePasswordAuthenticationToken(consoleUser.getEmail(), consoleUser.getPassword(), grantedAuthorities);
SecurityContextHolder.getContext().setAuthentication(authentication);
Assert.assertTrue(true);
}
}
}
So I ran my unit test suite (1 struts action and 1 Spring test), and the following exception gets thrown in my logs:
2974 [main] ERROR org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'messageSource' defined in class path resource [spring-messagesource-config.xml]: Cannot resolve reference to bean 'messageBundleLocation' while setting bean property 'basename'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'messageBundleLocation' defined in class path resource [spring-messagesource-config.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.googlecode.ehcache.annotations.config.internalEhCacheCachingAdvisor': Cannot resolve reference to bean 'com.googlecode.ehcache.annotations.impl.CacheStaticMethodMatcherPointcut#0' while setting bean property 'pointcut'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.googlecode.ehcache.annotations.impl.CacheStaticMethodMatcherPointcut#0': Cannot resolve reference to bean 'com.googlecode.ehcache.annotations.impl.CacheAttributeSourceImpl#0' while setting bean property 'cacheAttributeSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.googlecode.ehcache.annotations.impl.CacheAttributeSourceImpl#0': Cannot resolve reference to bean 'facadeCacheManager' while setting bean property 'cacheManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'facadeCacheManager' defined in class path resource [spring-cache-config-test.xml]: Invocation of init method failed; nested exception is net.sf.ehcache.CacheException: Another CacheManager with same name 'API_CACHE_MANAGER' already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following:
1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary
2. Shutdown the earlier cacheManager before creating new one with same name.
The source of the existing CacheManager is: InputStreamConfigurationSource [stream=sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream#327df0b7]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:329)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:107)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1387)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1128)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
at org.springframework.context.support.AbstractApplicationContext.initMessageSource(AbstractApplicationContext.java:786)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:467)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)
at BaseStrutsTestCase.setUp(BaseStrutsTestCase.java:80)
at junit.framework.TestCase.runBare(TestCase.java:139)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at junit.framework.TestSuite.runTest(TestSuite.java:255)
at junit.framework.TestSuite.run(TestSuite.java:250)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:345)
at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1009)
Is there a way to run these tests one after another, either by persisting the beans already created by my Spring unit tests or destroying the context configuration after the Spring unit test is done running and then having Struts use the context configuration within BaseStrutsTestCase?

EJB Timer Service error

I'm trying to use the EJB timer service, I have a class named TimerBean which carries the methods to schedule a timer and handle the timeout, this implements TimerBeanRemote an interface class.
In another session bean I have the following:
TimerBeanRemote service = (TimerBeanRemote) new InitialContext().lookup("TimerBean/remote");
When I try to run it on the server I get the error:
javax.naming.NamingException: Lookup failed for 'TimerBean/remote' in SerialContext [Root exception is javax.naming.NameNotFoundException: TimerBean]
Any ideas as to why it can't find it? Thanks!
Following from your comments -if you are trying to access TimerBeanRemote within the same container then you can inject the #Remote ejb in your servlet or JSF Backing Bean else you can locate your EJB through a JNDI lookup.
Suppose your TimerBean is: com.mypackage.timer.TimerBeanRemote
then as per explanation above you can either inject or lookup:
Injection
public class MyServlet ...{
#EJB
com.mypackage.timer.TimerBeanRemote timerBean;
}
JNDI lookup:
Properties props = new Properties();
props.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
String[] serverDetails = server.split(":");
props.setProperty("org.omg.CORBA.ORBInitialHost", MyHost);
props.setProperty("org.omg.CORBA.ORBInitialPort", MyPort);
InitialContext ic = new InitialContext(props);<br>
TimerBeanRemote timerBean = (TimerBeanRemote)ic.lookup("com.mypackage.timer.TimerBeanRemote");
You can read the following articles for further details:
http://download.oracle.com/javaee/1.4/tutorial/doc/Session5.html
http://www.javabeat.net/articles/3-ejb-30-timer-services-an-overview-1.html

Categories