Spring - Passing Listener via RMI - java

Basis
I have several projects, which implement a caching service - each its own, and each one is almost the same as the others. I have coompressed and shortened the existing cacheservices into a single project containing two different cache services - one for basic objects, and one for complex objects (which extends the basic service). I have created 2 projects - one to deploy as a .war-file and the API-project for this.
The entire application runs on a JBoss AS 7.1 "Thunder" with the latest JDK 7.
Problem
One class in particular requires to be notified whenever an old cache entry is deleted. For this, I implemented a notification procedure using CleanupListeners.
CleanupListener.java
public interface CleanupListener extends Remote {
public abstract boolean supports(Class<?> clazz) throws RemoteException;
public abstract void notify(Object removedObject) throws RemoteException;
}
necessary implementations in the cache service
public void registerCleanupListener(CleanupListener listener) {
listeners.add(listener);
}
private void fireCleanupEvent(Object removedObject) {
for (CleanupListener cleanupListener : listeners) {
try {
if (cleanupListener.supports(removedObject.getClass())) {
cleanupListener.notify(removedObject);
}
} catch (RemoteException re) {
LOGGER.error("Remote Listener not available", re);
}
}
}
The listeners are managed via a private final HashSet<CleanupListener>
The project which needs to be notified uses this implementation of the interface:
#Component
public class CleanupEventListener extends UnicastRemoteObject implements CleanupListener {
protected CleanupEventListener() throws RemoteException {
super();
}
#Resource
private ClassRequiresNotification notifyMe;
public boolean supports(Class<?> clazz) {
return SupportedObject.class.isAssignableFrom(clazz);
}
public void notify(Object removedObject) {
notifyMe.someMethod((SupportedObject) removedObject);
}
}
and a registration bean:
#Component
public class CleanupEventListenerRegistrator {
#Resource
private CleanupEventListener cleanupEventListener;
#Resource
private BasicCacheService cacheService;
#PostConstruct
public void init() {
cacheService.registerCleanupListener(cleanupEventListener);
}
}
The remote services are exported via SpringBean:
#Bean
public RmiServiceExporter basicCacheServiceExporter(BasicCacheService basicCacheService) {
RmiServiceExporter cacheService = new RmiServiceExporter();
cacheService.setService(basicCacheService);
cacheService.setServiceName(BASIC_CACHE_SERVICE_NAME);
cacheService.setServiceInterface(some.package.BasicCacheService.class);
return cacheService;
}
and registered via XML-Config:
<bean id="basicCacheService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="lookupStubOnStartup" value="false"/>
<property name="serviceUrl" value="basicCacheService" />
<property name="serviceInterface" value="some.package.BasicCacheService" />
<property name="refreshStubOnConnectFailure" value="true"/>
</bean>
Exception
So, whenever I try to call the expected methods, I get one of two Exceptions:
Either I get a NoSuchObjectException (rare) or, much more common a ClassNotFoundException wrapped in an UnmarshalException
What I did
I already consulted the JavaDocs, googled massively and have sifted through several questions here, however to no avail. I found a bit of info that the standard property rmi.codebase is set to true, and this might require some rewriting of code, however we are already using RMI at another place in the projects, and it does work fine there. Adding a SecurityManager breaks the rest of our Application (due to external prerequisites) and calls the NoSuchObjectException.
Answers within StackOverflow so far have yielded "this is a problem with the codebase", "you need to install a securityManager", "the classes have to have the same name and be in the same package" and of course "it's the codebase". I am mentioning this one twice, since it does appear quite often yet not a single mention as to how to deal with a codebase problem accompanies it.
Additional Info
I am not sure if this is important, but here's some more info:
The cacheservice-project is configured in java code, the projects which use the service are configured in XML.
The Listener-Interface is not exported vie Annotations or XML-Beans, since several Posts (including at least one here) hinted that this will export the class, but keep listening only on server-side, which is not what I want.
I have also tried to make the CleanupListeneran abstract class, which in itselt extends UnicastRemoteObject but that did not work out either - in fact, it was worse than what I have now.
I am writing and testing the application in eclipse.
Assembling and publishing to server is done by gradle in the command line.
Question
Does anyone know what the problem here is, and how to actually solve it? It is getting quite aggravating when everything I do is either running into an exception or is "yeah that's a codebase error", since both do not help at all.
Any help would be appreciated.

Actual answer
Okay, this is simultaneously embarrassing and relieving. First of all...
The good news
The Listener-Interface as described above works. It is registered on the server-side, and when called, fires a notification to the client, which then may act accordingly. So, if you have been looking for an answer to the whole "passing a listener"-problem, there you have it.
The bad news
however is that I have been quite ignorant to certain facts. Among other things, what the term codebase actually covers. In my particular case, the problem was that the cacheservice itself had no dependencies on the other projects, which (custom) classes it should preserve. So technically, the codebase actually was the problem - due to lacking dependencies in the build.gradle.
I hope I dodn't waste anyone's time on this, if so, please accept my apologies, and hopefully, this question helps someone else sometime.

Related

setServletContext not activating inside #Configuration file

So, this is something of a follow-on of this question. My current code looks something like this:
#Configuration
#EnableAutoConfiguration
#ComponentScan(basePackages = {"base.pkg.name"})
public class MyApp implements ServletContextAware {
private ThingDAO beanThingDAO = null;
public MyApp() {
// Lots of stuff goes here.
// no reference to servletContext, though
// beanThing gets initialized, and mostly populated.
}
#Bean publicD ThingDAO getBeanThingDAO() { return beanThingDAO; }
public void setServletContext(ServletContext servletContext) {
// all references to servletContext go here, including the
// bit where we call the appropriate setters in beanThingDAO
{
}
The problem is, it's not working. Specifically, my understanding was that setServletContext was supposed to be called by various forms of Spring Magic at some point during the startup process, but (as revealed by System.out.println()) it never gets called. I'm trying to finish up the first stage of a major bunch of refactoring, and for the moment it is of notable value to me to be able to handle the access to servletContext entirely inside of the #Configuration file. I'm not looking for an answer that will tell me that I should put it in the controllers. I'm looking for an answer that will either tell me how to get it working inside of the #Configuration file, or explain why that won't work, and what I can do about it.
I just ran into a very similar issue and while I'm not positive it's exactly the same problem I thought I'd record my solution in case it's helpful to others.
In my case I had a single #Configuration class for my spring boot application that implemented both ServletContextAware and WebMvcConfigurer.
In the end it turns out that Spring Boot has a bug (or at least undocumented restraint) that ServletContextAware.setServletContext() will never be called if you also implement WebMvcConfigurer on the same class. The solution was simply to split out a separate #Configuration class to implement ServletContextAware.
Here's a simple project I found that demonstrates and explains more what the problem was for me:
https://github.com/dvntucker/boot-issue-sample
The OP doesn't show that the bean in question was implementing both of these, but given the OP is using simplified example code I thought maybe the fact that the asker could have been implementing both interfaces in his actual code and might have omitted the second interface.
Well, I have an answer. It's not one I'm particularly happy with, so I won't be accepting it, but if someone with my same problem stumbles across this question, I want to at least give them the benefit of my experience.
For some reason, the ServletContextAware automatic call simply doesn't work under those circumstances. It works for pretty much every other component, though. I created a kludge class that looks something like this:
// This class's only purpose is to act as a kludge to in some way get
// around the fact that ServletContextAware doesn't seem to work on MyApp.
// none of the *other* spring boot ways of getting the servlet context into a
// file seem to work either.
#Component
public class ServletContextSetter implements ServletContextAware {
private MyApp app;
public ServletContextSetter(MyApp app) {
this.app = app;
}
#Override
public void setServletContext(ServletContext servletContext) {
app.setServletContext(servletContext);
}
}
Does the job. I don't like it, and I will be rebuilding things later to make it unnecessary so I can take it out, but it does work. I'm going to hold the checkmark, though, in case anyone can tell me either how to make it work entirely inside the #Configuration - decorated file, or why it doesn't work there.
Note that the #Component decorator is important, here. Won't work without it.

Listening to class reload in Java

For performance reasons, I have a class that stores a Map whose key is a Class<?> and its value is function of that class's fields. The map is populated during code execution according to the type of the calling object. The above is a generalization/simplification
public class Cache {
private static final Map<Class<?>, String> fieldsList = ...;
//Synchronization omitted for brevity
public String getHqlFor(Class<?> entity){
if (!fieldsList.containsKey(entity))
fieldsList.put(entity,createHql(entity));
return fieldsList.get(entity);
}
}
During development, thanks to the help of Jrebel, I often make modifications to classes by changing entire properties or just their names. I can continue development just fine. However, if I already put a value into the cache it will be stale forever.
What I am asking here is if it is possible to intercept the event that a class in the classpath has changed. Very broad... But my specific problem is very simple: since I have such a need only during development, I just want to wipe that cache in case any class in my classpath changes.
How can I accomplish this? I don't need to do anything special than intercepting the event and simply wiping the cache
JRebel has a plugin API that you can use to trigger code on class reloads. The tutorial complete with example application and plugin available here: https://manuals.zeroturnaround.com/jrebel/advanced/custom.html
The JRebel plugin is a self-contained jar built against the JRebel SDK, which is attached to the running application via the JVM argument -Drebel.plugins=/path/to/my-plugin.jar. The JRebel agent attached to the application will load and start plugins from this argument.
If the application is not started with the JRebel agent, the plugin is simply not loaded.
In your example you want to register a ClassEventListener that will clear the Cache.fieldsList map. As it is a private field, you need to access it via reflection or add a get/clear method via a ClassBytecodeProcessor
public class MyPlugin implements Plugin {
void preinit() {
ReloaderFactory.getInstance().addClassReloadListener(new ClassEventListenerAdapter(0) {
#Override
public void onClassEvent(int eventType, Class<?> klass) throws Exception {
Cache.clear();
}
});
}
// ... other methods ...
}
And to clear the map
public class CacheCBP extends JavassistClassBytecodeProcessor {
public void process(ClassPool cp, ClassLoader cl, CtClass ctClass) {
ctClass.addMethod(CtMethod.make("public static void clear() { fieldsList.clear(); }", ctClass));
}
}
However a better option is to only clear/recalculate the single class entry on class reload if possible. The example didn't display whether the info computed from one class depended on superclass infos, but if this is true, the JRebel SDK has methods to register a reload listener on the class hierarchy as well.
There is an existing class ClassValue which already does the job for you:
public class Cache {
private final ClassValue<String> backend = new ClassValue<String>() {
#Override
protected String computeValue(Class<?> entity) {
return createHql(entity);
}
};
public String getHqlFor(Class<?> entity){
return backend.get(entity);
}
}
When you call get, it will call computeValue if this is the first call for this specific Class argument or return the already existing value otherwise. It does already care thread safety and for allowing classes to get garbage collected. You don’t need to know when class unloading actually happens.

Persisting my state between uses

Newbie at netbeans-platform.
How can I save my state from one execution to the next.
The netbeans platform elegantly remembers the state and position of all my windows. How can I add to that state some of my own data? Very much like Netbeans saves what projects are open and reopens them when it starts up, along with their state.
Ass suggested here I added the following to my TopComponent but it doesn't work. getPersistenceType is called but neither writeExternal n'or readExternal are called.
#Override
public int getPersistenceType() {
return TopComponent.PERSISTENCE_ALWAYS;
}
#Override
public void writeExternal(ObjectOutput oo) throws IOException {
super.writeExternal(oo);
}
#Override
public void readExternal(ObjectInput oi) throws IOException, ClassNotFoundException {
super.readExternal(oi);
}
Comments here suggest tapping into readProperties and writeProperties but that doesn't feel right to me. I am not wanting to store Properties, I want to store State.
Some years ago I blogged about this, using the Session Storage feature of the Swing Application Framework in a NetBeans Platform application:
http://puces-blog.blogspot.ch/2009/04/netbeans-platform-meets-swing.html
The following 3 classes should provide the integration into the NetBeans Platform:
ModuleApplicationContext.java
ModuleLocalStorage.java
Modules.java
The referenced XProperties and JXTable you only need if you want support for SwingX classes such as JXTable.
To use this feature in your own module you need to initialize the context in your ModuleInstall class:
public class Installer extends ModuleInstall {
private static ModuleApplicationContext applicationContext;
#Override
public void restored() {
applicationContext = new ModuleApplicationContext(Modules.getModuleInfo(
Installer.class));
}
public static ModuleApplicationContext getApplicationContext() {
return applicationContext;
}
}
For a given contentPane you can then store the GUI session state using:
Installer.getApplicationContext().getSessionStorage().save(
getContentPanel(), SESSION_STORAGE_XML);
and restore the state using:
Installer.getApplicationContext().getSessionStorage().
restore(getContentPanel(), SESSION_STORAGE_XML);
Note: you need to set the component names of the relevant components
You can find the complete sample here: http://sourceforge.net/p/puces-samples/code/HEAD/tree/tags/sessionstate-1.0/
Also note however that development of the Swing Application Framework (JSR-296) has been withdrawn.
There is a fork called Better Swing Application Framework, but I haven't used it yet.
I also had some problems with this but finally I could fix it.
Annotate Your topcomponent class with #TopComponent.Description and set the right persistence type inside the annotation.
Your topcomponent class has to be serializable so,
every fields inside the topcompent have to be serializable or transient.
You can implement Your custom serialization with readExtern/writeExternal but it is not necessary, You can remove them.
If it still does not work check the log after You closed Your netbeans app and You will see why the platform could not serialize Your topComponent.

Cannot call method in red5 server from AS3 Flash CS6

Okay, this had been making me very mad. I've followed almost 8 tutorials all over the Internet and in the end, I got my Red5 server instance working. Good for me! But when I'm calling my Java methods in my Red5 apps from my AS3 apps, in the 'Console' window in Eclipse, I got this error :
[ERROR] [NioProcessor-1] org.red5.server.service.ServiceInvoker - Method getTheName with parameters [] not found in org.red5.core.Application#17e5fde
Here's my Application.java file.
package org.red5.core;
import org.red5.server.adapter.ApplicationAdapter;
import org.red5.server.api.IConnection;
import org.red5.server.api.IScope;
import org.red5.server.api.service.ServiceUtils;
/**
* Sample application that uses the client manager.
*
* #author The Red5 Project (red5#osflash.org)
*/
public class Application extends ApplicationAdapter {
/** {#inheritDoc} */
#Override
public boolean connect(IConnection conn, IScope scope, Object[] params) {
return true;
}
/** {#inheritDoc} */
#Override
public void disconnect(IConnection conn, IScope scope) {
super.disconnect(conn, scope);
}
public String getTheName() { return "MyName!"; }
}
And here's my AS3 code. I just put this on the Timeline.
var nc:NetConnection = new NetConnection();
nc.connect("http://localhost/Mintium/RoomHere", "SomeUsernameHere");
nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
nc.objectEncoding = ObjectEncoding.AMF0;
function onNetStatus(e:NetStatusEvent):void
{
switch (e.info.code)
{
case "NetConnection.Connect.Success" :
trace("connected");
nc.call("getTheName", new Responder(getName_result, getName_error));
break;
}
}
function getName_result(res:Object):void { append("Name : " + res.toString()); }
function getName_error(res:Object):void { append(res.toString()); }
Its been a week I've been trying to figure it out and my dateline is next month. If this stuff is not solved, I'm gonna fail my assessment. Please help me with my problems. Thank you very much.
Sorry I did not see this 2 months ago, I could have helped you pass your assessment. Nevertheless, I think I can answer this question, having had a similar problem calling Red5 services.
The key to solving this problem is in those parts of Red5 that utilize the Spring Framework. In your project, there should be a file called red5-web.xml that resides in the Server project's WEB-INF folder. This file contains some Bean dependencies used by Red5's Spring components. This is not mentioned in the tutorials that I read, or even in most of the (rather sparse and distributed) red5 programming documentation.
What you have to do is add a bean entry for your method in that file. In your case, the entry should look like this:
<bean id="getTheName.service" class="org.red5.core.Application" />
Note my use of the name of your function, with ".service" appended. I do not understand why, but you need the ".service" appended in order for Red5 to find your function. You need to add a similar entry for every class whose functions you want to use as services.
Of course, I based everything I said above on the fact that you put the service into the Application class -- something which I never do. if you read the red5-web.xml file, you will see that there is already an entry for that class, because it is already injected through Spring as the class that acts as an "endpoint" for processing requests over the web. I do not know if using the Application class as an endpoint and a provider of services is a good idea (it violates "separation of concerns" in OOP and may cause problems with Spring).
What I usually do is add a separate class in the org.red5.core package (or any other package you might want) that acts to deliver the desired service, then put an entry into red5-web.xml that injects the class and its method. So, for your project, lets assume you have a class called NameProvider in the org.red5.core package:
public class NameProvider
{
public NameProvider() {}
public String getTheName() { return("MyName!"); }
}
then you add the following entry to your red5-web.xml file:
<bean id="getTheName.service" class="org.red5.core.NameProvider" />
That should make everything work.
I hope this helps you in the future, or anyone else having this problem. I just wish I'd seen this question sooner.

Is it possible to make web app proactive rather than reactive?

Web applications traditionally follow the request/response cycle, where a request is made by a user or another web app. However, I'm curious if it is possible to make a web app automatically initiate certain tasks upon it's deployment to a app server. For example, let's say we have a web app that retrieves and processes data. Is it possible to configure this app to automatically retrieve and process data when certain criteria are met, rather than needing a request from a user/another web app?
Yes it is using task schedulers like cron for example. Although one might argue, that such tasks no longer are a web application, but local applications unning on the server instead.
Have a look at Quartz Scheduler
If the data in your example are in files, then you can use a FileWatcher to monitor the folder and initiate some action when a new file lands there.
Here is one example of a very simple one, just to give you an idea of how it works:
package pilrcedit;
import java.io.*;
import java.util.*;
public class FileWatcher implements ChangeListener
{
private static final int FILE_MODIFIED=MODIFIED;
Hashtable files=new Hashtable();
ChangeListeners listeners=new ChangeListeners();
public FileWatcher()
{
Preferences.addChangeListener(this);
}
public void objectChanged(Object source,int id,Object data)
{
checkFiles();
}
private void checkFiles()
{
for(Enumeration e=files.keys();e.hasMoreElements();)
{
File f=(File)e.nextElement();
if (f.lastModified()!=((Long)files.get(f)).longValue())
{
files.put(f,new Long(f.lastModified()));
notifyListeners(f);
}
}
}
public void addFile(File f,ChangeListener listener)
{
files.put(f,new Long(f.lastModified()));
listeners.add(f,listener);
}
public void removeFile(File f,ChangeListener listener)
{
listeners.remove(f,listener);
}
public void notifyListeners(File f)
{
listeners.fireObjectChanged(f,this,FILE_MODIFIED,f);
}
}
You can include that in the deploy process itself. Or include that initial request to the webpage in the deploy process.
Since you mentioned you want to do certain things during the deployment I think you can use spring here to perform certain tasks or load something in memory (cache) during deployment. For example in the application context xml you can have this:-
<bean id="someCache" class="com.my.company.MyCache"
init-method="load">
<!-- <property> as needed -->
</bean>
MyCache class could be something like below:-
class MyCache{
public void load() {
//do your deployment work
}
}

Categories