EJB Remote Method Invocation with client - ejb

I have got a problem with ejb remote method invocation. For testing I created a ejb project with a statless bean and a simple method, declared in a remote interface. I deployed this project in a JBoss 7.1.
Now I want to call the business method hello in my bean from another class, which is not part of the project and not deployed in that jboss server. So if I understand it correct, this is a normal remote method call from a client.
I wrote my HelloWorldClient, declared the properties for the InitialContext(). But when I run this client, I got a NameNotFoundException. For me it seems like the exception is because of a false jndi-name, but I tried a lot to get the correct one.
On the console I got jndi names from my server for the project, but it doesn't work. Could someone help me solve this problem?
Client with context:
Properties jndiProps = new Properties();
jndiProps.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
jndiProps.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
jndiProps.put(Context.PROVIDER_URL,"remote://localhost:4447");
jndiProps.put(Context.SECURITY_PRINCIPAL, "testuser");
jndiProps.put(Context.SECURITY_CREDENTIALS, "testpasswort");
Context ctx = new InitialContext(jndiProps);
HelloWorldRemote hello = (HelloWorldRemote) ctx.lookup("java:global[/ejbproject1]/ejbproject1/HelloWorldBean[/HelloWorldRemote]");
My Bean:
#Stateless(name="hello")
public class HelloWorldBean implements HelloWorldRemote{
#Override
public String hello(String value) {
System.out.println("Say hello to "+value);
return "Hello " + value;
}
the jndi names from the jboss
18:19:07,464 INFO [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-6) JNDI bindings for session bean named hello in deployment unit deployment "ejbproject1.jar" are as follows:
java:global/ejbproject1/hello!interfaces.HelloWorldRemote
java:app/ejbproject1/hello!interfaces.HelloWorldRemote
java:module/hello!interfaces.HelloWorldRemote
java:jboss/exported/ejbproject1/hello!interfaces.HelloWorldRemote
java:global/ejbproject1/hello
java:app/ejbproject1/hello
java:module/hello
and the exception:
javax.naming.NameNotFoundException: global[/ejbproject1]/ejbproject1/HelloWorldBean[/HelloWorldRemote] -- service jboss.naming.context.java.jboss.exported.global[.ejbproject1].ejbproject1.HelloWorldBean[.HelloWorldRemote]

Change from
java:global/ejbproject1/hello!interfaces.HelloWorldRemote
to
java:/ejbproject1/hello!interfaces.HelloWorldRemote
That will work.

Related

How to create a WebClient-object in a spring application with oauth2

I'm developing a spring application (client) that is secured with an OAuth2 provider. This application should do some REST calls to another spring application (resource server). For performing the REST calls, I will use spring's WebClient.
I therefore try to create a bean of type WebClient as can be found in several blogs.
#Configuration
public class AppConfig {
#Bean
public WebClient webClient(ReactiveClientRegistrationRepository clientRegistrations) {
ServerOAuth2AuthorizedClientExchangeFilterFunction oauth =
new ServerOAuth2AuthorizedClientExchangeFilterFunction(clientRegistrations,
new UnAuthenticatedServerOAuth2AuthorizedClientRepository());
oauth.setDefaultClientRegistrationId("myprovider");
return WebClient.builder().filter(oauth).build();
}
}
When starting the application, I get the following error:
The following candidates were found but could not be injected:
- Bean method 'clientRegistrationRepository' in 'ReactiveOAuth2ClientAutoConfiguration' not loaded because NoneNestedConditions 1 matched 0 did not; NestedCondition on ReactiveOAuth2ClientAutoConfiguration.NonServletApplicationCondition.ServletApplicationCondition found 'session' scope
Action:
Consider revisiting the entries above or defining a bean of type 'org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository' in your configuration.
As several websites recommend exactly this code for generating a WebClient instance when using OAuth2 authentication, I'm wondering what I'm doing wrong?
Do you have any suggestions for me?
Thanks.
I got the same issue. I changed the code as provided in the video : https://www.youtube.com/watch?v=1N-xwmoN83w&t=1569s and that worked
#Bean
public WebClient webClient(ClientRegistrationRepository clientRegistrationRepository , OAuth2AuthorizedClientRepository authorizedClientRepository) {
ServletOAuth2AuthorizedClientExchangeFilterFunction oauth =
new ServletOAuth2AuthorizedClientExchangeFilterFunction (clientRegistrationRepository , authorizedClientRepository);
return WebClient.builder().apply(oauth.oauth2Configuration()).build();
}
Hope that helps.

ClassCastException while call EJB over JNDI

I'm getting an ClassCastException while calling an EJB in an Pojo over JNDI. I use Oracle Weblogic Server 10.3.6 (EJB 3.0).
My structure:
app.ear
lib
Interfaces.jar
MyBeanInterface.java
ejb.jar
MyBeanImpl.java
webapp.war
Client.java
WEB-INF
web.xml
My local Interface:
package mypackage;
#Local
public Interface MyBeanInterface {}
My EJB-Class:
package mypackage;
#Stateless(name = "MyBean")
public class MyBeanImpl implements MyBeanInterface {}
My Client (not an EJB):
MyBeanInterface bean = (MyBeanInterface) new InitialContext().lookup("java:comp/env/ejb/MyBean");
My web.xml
<ejb-local-ref>
<ejb-ref-name>ejb/MyBean</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local>mypackage.MyBeanInterface</local>
</ejb-local-ref>
My Exception:
The Lookup itself works. I get a reference. But when I want to cast with (MyBeanInterface) I get the following error:
Cannot cast an instance of "class mypackage.MyBeanInterface_whjkp6_MyBeanImpl (loaded by instance of weblogic.utils.classloaders.GenericClassLoader(id=28136))" to an instance of "interface mypackage.MyBeanInterface(loaded by instance of weblogic.utils.classloaders.GenericClassLoader(id=28144))
What can I do?
It seems the classes are loaded by different class loaders. Possible options are:
1) Ensure the classes are loaded by same class loader
2) Use reflection
3) Serialize and then deserialize
Refer:
1) cast across classloader?
2) https://community.oracle.com/thread/757133
3) ClassCastException because of classloaders?

JBoss AS 7, EJB3 cast to interface

I'm facing weird behavior with JBoss AS 7 and my application which uses EJB3.1.
I successfully lookup bean but when Im trying to cast it to its interface, exception is thrown.
Code in short:
#Local
public interface BusinessObjectsFactory { ... }
#Stateless
#Local(BusinessObjectsFactory.class)
public class JPABusinessObjectsFactory implements BusinessObjectsFactory { ... }
...
Object obj = ctx.lookup("java:app/moduleName/" +
"JPABusinessObjectsFactory!pckg.BusinessObjectsFactory");
Class c = obj.getClass();
System.out.println(c.getName()); // pckg.BusinessObjectsFactory$$$view36
System.out.println(c.getInterfaces()[0].getName()); // BusinessObjectsFactory
BusinessObjectsFactory bof = (BusinessObjectsFactory) obj; //cast exception
Any ideas? Note that interface is needed (which implementation is looked up is read from configuration file and might change)
I switched to another lookup strategy while this is no longer issue for me. I'm not sure if this is still present in newest versions of JBoss/Wildfly AS. That's why I'm closing this question.

Spring-Flex Session Scope Service

I am using Spring Flex project 1.5. I wish to create a service with session scope.
#Service("storeService")
#Scope(value = "session", proxyMode = ScopedProxyMode.INTERFACES)
#RemotingDestination(channels = { "my-amf" })
public class StoreService implements IStoreService {
When I access the service, I got No destination error.
org.springframework.flex.core.DefaultExceptionLogger - The following exception occurred during request processing by the BlazeDS MessageBroker and will be serialized back to the client:
flex.messaging.MessageException: No destination with id 'storeService' is registered with any service.
I have no issue if I do not annotate with #Scope, but it is singleton scope not session scope. Am I missing anything?
Problem is solved after I changed proxyMode to ScopedProxyMode.TARGET_CLASS.

OpenEJB : Configuring MDBs with custom ConnectionFactory

As described in OpenEJB docs, we can configure JMS connection factory and queues, and they will appear in JNDI as:
openejb:Resource/MyJmsConnectionFactory,
openejb:Resource/MyQueue
Given those JNDI entries, how can I tell to MDB to use them?
Is it possible to change JNDI name, for example ConnectionFactory to appear as java:/ConnectionFactory
or ConnectionFactory
Things work differently than you may be imagining. Specifying that an MDB is tied to a javax.jms.Queue and the name of that queue is part of the EJB specification and done via the ActivationConfig, like so:
#MessageDriven(activationConfig = {
#ActivationConfigProperty(
propertyName = "destinationType",
propertyValue = "javax.jms.Queue"),
#ActivationConfigProperty(
propertyName = "destination",
propertyValue = "FooQueue")})
public static class JmsBean implements MessageListener {
public void onMessage(Message message) {
}
}
The MDB container itself is not actually JMS-aware at all. It simply understands that it should hook the bean up to a specific Resource Adapter.
<openejb>
<Resource id="MyJmsResourceAdapter" type="ActiveMQResourceAdapter">
ServerUrl tcp://someHostName:61616
</Resource>
<Container id="MyJmsMdbContainer" ctype="MESSAGE">
ResourceAdapter MyJmsResourceAdapter
</Container>
</openejb>
The above shows an MDB Container hooked up to a Resource Adapter that uses JMS via ActiveMQ.
Here is an example that shows an MDB Container hooked up to a Quartz Resource Adapter
It isn't possible to tell the MDB Container about JMS specific things as per specification, the relationship is much more generic than that. This blog post gives some insight as to how things work.

Resources