I am new to EJB, and I have a question regarding MDB.
How do I convert MDB stateless beans to stateful beans? Options:
using bean passivation only.
using bean activation only.
using both activation and passivation.
None of the above.
Thanks in advance.
The answer is 4) None of the above.
There is no "Stateful Message Driven Bean". A Message Driven Bean is always stateless, because it cannot hold a conversational state. It's like a stateless bean with the difference, that it handles JMS messages.
If you are a beginner, the free book Mastering EJB may help you to understand EJB more in detail.
Related
In a web application, users use a servlet HTTP session. Some data are stored in CDI #SessionScoped beans. Later in some pages WebSocket communications are performed between the user browser and the server.
With GlassFish 4, when using an injected #SessionScoped CDI bean from a WebSocket #ServerEndpoint with GlassFish 4.0 I get an error message:
org.jboss.weld.context.ContextNotActiveException: WELD-001303 No active contexts for scope type javax.enterprise.context.SessionScoped
From JSP/servlet type requests, using the #SessionScoped CDI bean is fine, but not from the WebSocket #ServerEndpoint.
Also note that using #Singleton CDI beans from the #ServerEndpoint works fine, so this is not a general purpose dependency injection problem.
Maybe I did something wrong.
So my questions is can #SessionScoped CDI beans be used from methods of WebSocket #ServerEndpoint beans? If yes, how?
Thank you for your time.
It may not be the exact same question, but the issue is similar enough that the answers there apply here. Basically, as #JoakimErdfelt notes websocket support for CDI is problematic at best. The websocket spec neglected to mention what scopes are active.
Out of the box, this is what Tyrus supports: https://github.com/tyrus-project/tyrus/tree/master/samples/cdi/src/main/java/org/glassfish/tyrus/sample/cdi
If you want, you can extend it to start a session scope (for reference, Apache DeltaSpike's CDI Context Control), but because of the protocol difference it would be a different session than the one already established via HTTP.
I have a Spring bean with scope session. This bean holds a reference to another singleton bean which is not serializable. What is the best approach if I want to serialize the session scoped bean?
The same question is already asked here: Spring session-scoped beans (controllers) and references to services, in terms of serialization
The accepted answer is that:
[...]this issue is resolved in spring 3.0 by providing a proxy of non-serializable beans, which obtains an instance from the current application context
As far as I understand the speaker in the linked video it should "just work". But in my case it doesn't! When I try to serialize my session scoped bean i get a NotSerializableException.
How can I solve this problem?
You need to instruct Spring to create that proxy. In XML-based config, via <aop:scoped-proxy/> tag, in component-scan mode via annotation:
#Scope(proxyMode = ScopedProxyMode.INTERFACES)
on your controller class.
You may mark singleton reference field as transient. Then check How to execute method after deserialization and load reference from ApplicationContext.
Also, please provide stacktrace.
P.S.
It is not too good idea to use session passivation.
Spring beans have 2 types of scopes (if you do not take into account scopes for web): singleton, which is default and prototype. Roughly they implement singleton and prototype design patterns within context.
So if the bean has prototype scope it can hold a unique state like the stateful bean in EJB. When the scope of the bean is singleton the container will create only one instance of it. So we can say that singleton beans in EJB 3.1 is the analogue of singleton beans in spring.
But how I can get the features of stateless beans in Spring (I'm referring to pooling of stateless beans in EJB containers and about that each thread has a unique instance of the stateless bean)?
Either you can make the bean thread-safe, and a singleton bean is OK (that's the majority of the cases).
Or you can't, and you'll have to use a prototype. The difference I see between Spring prototype beans and stateless EJB session beans is that stateless session beans are pooled. But in these ages, pooling them or creating a new instance each time won't make much difference. Creating a new instance each time might even help the GC.
In an MDB application, Can the EJB3 Interceptor be configured to log the Non bean class methods, which are inturn invoked by the bean method (OnMessage)?
Thanks in advance
No, EJB interceptors are only applicable to business methods. For non-business methods, you need to use an aspect-oriented programming technology.
I am reading spring through its official documentation and at one place I came to a line that uses prototype scope for all stateful beans while singleton for stateless beans.
I know there is something as stateful as well as stateless beans in EJB but this is not what they have mentioned in the documents.
Can anyone explain to me what exactly this means of stateful as well stateless beans in Spring
Thanks in advance
From spring perspective
stateless beans: beans that are singleton and are initialized only once. The only state they have is a shared state. These beans are created while the ApplicationContext is being initialized. The SAME bean instance will be returned/injected during the lifetime of this ApplicationContext.
stateful beans: beans that can carry state (instance variables). These are created EVERY time an object is required (like using the "new" operator in java).
These are not EJB statfull/stateless session beans.
There is no point in storing specific information like client data, per request data in Singleton bean as they are created only once by Spring IOC container. That's why singleton beans are stateless. They are shared resources. They can only be used for storing global information.
When a request is made for creating a prototype bean, a new request is created every time. So, they can be used to store some specific information for each request. So they are stateful.
It totally depends on the implementation. See tomee for example http://tomee.apache.org/statelesscontainer-config.html . You'll have to check in your server docs