I'm trying to play around with EJB remote call but I'm getting an error. I have a web app called CallerApp that calls a method in another web app called RecieverApp.
In the CallerApp I have a remote interface:
#Remote
public interface ControllerRemote {
public int perform(int i);
}
and the call is performed in this class:
public class Talker {
#EJB private ControllerRemote remote;
//constructor and invoke setRemote() method to set remote
private void setRemote() throws Exception{
Properties p = new Properties();
Context jndiContext = new InitialContext(p);
Object ref = jndiContext.lookup("java:global/RecieverApp/Controller!bean.ControllerRemote");
remote = (ControllerRemote) PortableRemoteObject.narrow(ref, ControllerRemote.class);
}
public void action(){
remote.perform(5);
}
}
RecieverApp is depoloyed on the same Glassfish server:
#Stateless
public class Controller implements Serializable, ControllerRemote{
#Override
public int perform(int i){
//return something
}
}
The interface in RecieverApp is exactly as the one in CallerApp:
#Remote
public interface CallerRemote{
public int perform(int i);
}
I'm getting the following exception:
SEVERE: javax.naming.NamingException: Lookup failed for 'java:global/RecieverApp/Controller!bean.ControllerRemote'
in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory,
java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl,
java.naming.factory.url.pkgs=com.sun.enterprise.naming}
[Root exception is javax.naming.NamingException: ejb ref resolution error for remote business interfacebean.ControllerRemote
[Root exception is java.lang.ClassNotFoundException: bean.ControllerRemote]]
What I'm doing wrong here?
PS: I'm using Glassfish 3.1 and both applications are deployed on the same server.
There are few things to consider:
check whether JNDI name java:global/RecieverApp/Controller!bean.ControllerRemote exists, there was nice JNDI browser in Glassfish 2.x, but they didn't put it in GF 3 (it should be in GF 4), but you still have good old command line: asadmin list-jndi-entries
check whether your CallerRemote interfaces are in same packages in both applications
there is no need to perform both injection (#EJB) and JNDI lookup, if your class Talker is container-managed (i.e. bean, servlet, etc.) then #EJB annotation will suffice, otherwise use only lookup
Related
I'm working on a Quarkus Extension. But I have some problems to inject CDI Beans.
In the runtime module I have this class:
#ApplicationScoped
public class CustomRestClientBuilder{
//#Produces - Removed
//#ApplicationScoped - Removed
public RestClientBuilder newBuilder(String url) throws MalformedURLException {
return RestClientBuilder.newBuilder().baseUrl(new URL(url));
}
}
In the deployment module, I have this class:
class RestClientExtensionProcessor {
private static final String FEATURE = "rest-client-extension";
#BuildStep
FeatureBuildItem feature() {
return new FeatureBuildItem(FEATURE);
}
#BuildStep
public AdditionalBeanBuildItem producer() {
return new AdditionalBeanBuildItem(CustomRestClientBuilder.class);
}
}
And the problem appears in testing classes. I put the Test in Deployment module and it fails (however, in runtime module it works).
public class CustomRestClientBuilderTest {
#Inject
private CustomRestClientBuilder customRest;
#RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class));
#Test
public void testGreeting() {
IDummyRestClient restClient = customRest.newBuilder("url")
.build(IDummyRestClient.class);
}
}
The error is:
Build failed due to errors
[error]: Build step io.quarkus.arc.deployment.ArcProcessor#validate threw an exception:
javax.enterprise.inject.spi.DeploymentException: javax.enterprise.inject.UnsatisfiedResolutionException:
Unsatisfied dependency for type CustomRestClientBuilder and qualifiers [#Default]
I've checked this post before:
Exposing CDI beans from Quarkus extensions
UPDATED
This way is working (adding the class I need for testing). Does it makes sense?
#RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(CustomRestClientBuilder.class,
IDummyRestClient.class));
Spring boot 1.5.3 project with test user-registry on H2 in memory DB
This is the Error Stacktrace
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityConfiguration': Unsatisfied dependency expressed through field 'myAppUserDetailsService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'SMRTUserService': Unsatisfied dependency expressed through field 'userInfoDAO'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'SMRTUserDAO': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory': Post-processing of FactoryBean's singleton object failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #2 of URL [file:/C:/temp/SMRT/target/test-classes/data.sql]: ....
Can someone help me to understand the problem? I can't solve this errors.
Test Controller
public class CustomerControllerTest extends AbstractControllerTest {
#Test
#WithMockUser(roles = "ADMIN")
public void testShow() throws Exception {
mockMvc.perform(get("/customer/list")
.contentType(APPLICATION_JSON_UTF8))
.andExpect(status().isOk());
}
}
AbstractControllerTest
#RunWith(SpringRunner.class)
#SpringBootTest(webEnvironment = WebEnvironment.MOCK)
#AutoConfigureMockMvc
public abstract class AbstractControllerTest extends AbstractTest {
#Autowired protected MockMvc mockMvc;
#Autowired private FilterChainProxy filterChainProxy;
#Autowired private WebApplicationContext webApplicationContext;
#Before
public void setup() throws Exception {
MockitoAnnotations.initMocks(this);
this.mockMvc = webAppContextSetup(webApplicationContext)
.dispatchOptions(true)
.addFilters(filterChainProxy).build();
}
}
SecurityConfiguration
#Configuration
#EnableWebSecurity
#EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
#Autowired private SMRTUserService myAppUserDetailsService;
#Autowired private BCryptPasswordEncoder bCryptPasswordEncoder;
#Bean
public BCryptPasswordEncoder passwordEncoder() {
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
return bCryptPasswordEncoder;
}
#Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(myAppUserDetailsService)
.passwordEncoder(bCryptPasswordEncoder);
}
}
SMRTUSerService
#Service
#Slf4j
public class SMRTUserService implements UserDetailsService {
#Autowired private ISMRTUserDAO userInfoDAO;
#Autowired private SMRTUserRepository smrtuserRepository;
...
}
Thanks
Well, your Exception already explains pretty well what the problem is:
Failed to execute SQL script statement #2 of URL [file:/C:/temp/SMRT/target/test-classes/data.sql]: ....
I asume you're importing some test data for your tests? There must be an error in your SQL statements.
if you are using keycloak for the authentification . you might get this error .
jhipster by default gives you 8090 as auth server , so you have to change it
Solution : 1 - start your keycloak server
2 - go to your jhipster project main->ressources->config->application.yml
change issuer ui by the port where your keycloak server is running : for example : issuer-uri: http://localhost:8080/auth/realms/demo
hope that was helpfull
I am upgrading from JBoss 7.1.1 to WildFly 8.1.0 and can't get rid of the error described below:
14:53:04,666 ERROR [org.jboss.as.ejb3.invocation] (default task-17) JBAS014134: EJB Invocation failed on component TransRbDAO for method public java.util.List de.bss.dm.kairos.db.kairosgui.TransRbDAO.findAll(): javax.ejb.EJBException: java.lang.IllegalStateException: JBAS011048: Failed to construct component instance
TransRbDAO is:
#Stateless
public class TransRbDAO extends AbstractDAO<TransRb> {
public List<TransRb> findAll() {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<TransRb> criteria = cb.createQuery(TransRb.class);
Root<TransRb> root = criteria.from(TransRb.class);
criteria.select(root);
return em.createQuery(criteria).getResultList();
}
}
with AbstractDAO like:
public class AbstractDAO<T> {
#Inject
#CsarGuiDBExtended
#PersistenceContext(unitName = "CSAR_GUI", type = PersistenceContextType.EXTENDED)
protected EntityManager em;
public T findById(Class<T> clazz, Object primaryKey) {
T i = em.find(clazz, primaryKey);
return i;
}
}
This construct works when using only #PersistenceContext(unitName = "CSAR_GUI"), except for the expected LazyInitializationException when accessing data on the JSF-page.
The root-cause for error above is:
Caused by: javax.naming.NamingException: JBAS011878: Failed to lookup env/de.bss.dm.kairos.db.kairosgui.AbstractDAO/em [Root exception is java.lang.ArrayIndexOutOfBoundsException]
at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:144)
at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:81)
Is this a bug in WildFly? Was this working only because of a bug in JBoss? Or am I doing something completely wrong?
Extended persistence context can only be used in stateful session beans.
See EJB 3.2 spec section 11.11.1.1 or this article:
http://docs.jboss.org/ejb3/app-server/tutorial/extended_pc/extended.html
It appears that the failing code was implemented in JBoss 7.2.0. At this time the setting
default-extended-persistence-inheritance was introduced. Along came the Method
static Map<String, ExtendedEntityManager> More ...getCurrentCall() {
ArrayList<Map<String, ExtendedEntityManager>> stack = currentSFSBCallStack();
Map<String, ExtendedEntityManager> result = null;
if (stack != null) {
result = stack.get(stack.size() - 1);
}
return result;
}
stack.get() was throwing the ArrayIndexOutOfBounds-Exception.
When setting default-extended-persistence-inheritance="DEEP" in standalone.xml and marking TransRbDAO #Stateful the error disappears.
I'm not sure whether it was a bug in JBoss 7.1.1 that I used, or if it is a bug since 7.2.0, clarification is appreciated ;)
I have a collection of Processor beans in my application along with a factory for creating them.
public abstract class Processor {
public Processor(String config) { .... }
public abstract void process() throws Exception;
}
public class Processor1 extends Processor {
public Processor1(String config) { super(config);..}
public void process() {....}
}
public Processor newProcessor(String impl, String config) {
// use reflection to create processor
}
Can I use CDI to replace the factory class/method? And instead use a #Produces?
I tried using the following to iterate or select the instance I wanted. But Weld tells me that allProcessorInstances.isUnsatisfied() == true. I had to create default no-args ctor in order for Weld to find my Processor subclasses.
#Inject #Any Instance<Processor> allProcessorInstances;
Is there any way to tell the CDI container to use the constructor I want it to use? Or am I thinking about this problem the wrong way?
To use the constructor you'd need to annotate it with #Inject, however, every param on the constructor must itself be a bean or something in the CDI scopes.
Using a producer method and having that take an InjectionPoint as a param, then having your configuration be part of an annotation would work.
I am working on Eclipse and I want to create an enterprise application using Glassfish and MySQL.
I created a Enterprise Application Project, with EJB and WEB modules, named WeatherEJB and WeatherWeb.
In the WeatherEJB project I generated entities from tables, using JPA and also I created a stateless remote session bean, called CountryDAO, which implements CountryDAOBean, in order to wrap over the generated entity Country.
In the WeatherWeb project I added references to the WeatherEJB project in the Java Build bath, project references and module dependencies.
Then, in the WeatherWeb project, I created a managed bean called CountryController (at 'request' scope), which looks like this:
import javax.ejb.EJB;
import model.Country;
import service.CountryDAO;
public class CountryController
{
#EJB
CountryDAO countryDao;
private Country country;
public CountryController()
{
country = new Country();
}
public String saveCountry()
{
String returnValue = "success";
try
{
countryDao.saveCountry(country);
}
catch (Exception e){
e.printStackTrace();
returnValue = "failure";
}
return returnValue;
}
public Country getCountry(){
return country;
}
public void setCountry(Country country){
this.country = country;
}
}
Although I can deploy successfully the application on Glassfish, when I try to access a jsf that uses the CountryController, I get the following errors:
type Exception report
message
descriptionThe server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: javax.faces.FacesException: com.sun.enterprise.InjectionException: Exception attempting to inject Unresolved Ejb-Ref managedBeans.CountryController/countryDao#jndi: service.CountryDAO#null#service.CountryDAO#Session#null into class managedBeans.CountryController
root cause
javax.faces.FacesException: javax.faces.FacesException: com.sun.enterprise.InjectionException: Exception attempting to inject Unresolved Ejb-Ref managedBeans.CountryController/countryDao#jndi: service.CountryDAO#null#service.CountryDAO#Session#null into class managedBeans.CountryController
root cause
javax.faces.FacesException: com.sun.enterprise.InjectionException: Exception attempting to inject Unresolved Ejb-Ref managedBeans.CountryController/countryDao#jndi: service.CountryDAO#null#service.CountryDAO#Session#null into class managedBeans.CountryController
root cause
com.sun.enterprise.InjectionException: Exception attempting to inject Unresolved Ejb-Ref managedBeans.CountryController/countryDao#jndi: service.CountryDAO#null#service.CountryDAO#Session#null into class managedBeans.CountryController
root cause
javax.naming.NameNotFoundException: service.CountryDAO#service.CountryDAO not found
What am I missing? or what am I doing wrong?
Actually, instead of the implementation class:
#EJB
CountryDAO countryDao;
I should have used the interface:
#EJB
CountryDAOBean countryDao;