PDA

View Full Version : Java plugin questions and mutlithreading issues



terryhanks
02-22-2005, 10:41 AM
Hi,

I recently downloaded your server and wish to develop games using flash mx. My experience is mainly server side java. Servlets and EJBs. I am new to flash and actionscript , but I have had experience with javascript though mainly for form validation. Please excuse my spelling.
By the way the examples of Java plugins that you show in this forum look very straight forward.

Your plugins have no main class – do they run as threads in your server?

Are there multithreading issues in developing Java plugins?

I assume that it is best to use method variables, and to expect that no state is maintained between invocations of a plugin?

Can either room or server plug-ins maintain any type of state- other than writing to a file?

If a structure can be shared and updated across plugins, then I quess that is should be synchronized? Say use a hashtable rather than a hashmap?

Are there ways to serialize access to a shared object? My experience with multithreading is were I either create my own threads and take care of synchronization, or I use ejbs and let the ejb container take care of threading issues.

Do plug-in setInterval events correspond to Java timer threads?

If multiple plug-ins share access to a file that they append with writeFile, does a plugin have a write lock during the append? I experience this kind of issue with log4j daily appender.

Can plug-ins create their own i/o streams?

If I create a simple standalone http client class and import it, can my java plugin call an HTTP servlet running on the same host? – I might have tomcat or jboss running on the host.

I assume that plug-ins can use helper classes as long as you import them?

Can I use almost any java library that I place in the server lib directory in my plugin? If I place a jar file in the server lib file, must I modify the electroserver classpath to access it?

Is it a bad idea to spawn a thread in a plug-in? Would be like spawning a thread in a thread?

Sorry to be asking so many questions. I am really impressed with the quality of the games at your site, and with what I can see of some of the example applications that you show.

Thanks for your help.

Terry

webgeek
02-23-2005, 03:41 AM
Wow, that's quite the post! Let me first appologize for not having more Java documentation. We plan on correcting this issue with the next major release of the server. In the meantime, let me answer your questions.

>> Your plugins have no main class – do they run as threads in your server?
No, the plugins are invoked as needed by threads from within the thread pool. It's best to not assume that a single thread will always run the same plugin or that it will only run one plugin at a time. The answer to your fourth question will explain a lot more about this.

>> Are there multithreading issues in developing Java plugins?
There are no issues out of the gate but there most certainly can be once you start adding in code. You are required to synchronize as appropriate when dealing with any shared data.

>> I assume that it is best to use method variables, and to expect that no state is maintained between invocations of a plugin?
Not at all. Plugins wouldn't be very useful if that were the case. The answer to your next question explains this in more detail.

>> Can either room or server plug-ins maintain any type of state- other than writing to a file?
Certainly! A room plugin is instantiated when a room is created and will continue to exist as long as the room does. Init will be invoked when the room is created, destroy will be invoked when the room is droped. A server level plugin works the same but is invoked at server startup and will exist as long as the server is running. This allows you to use instance data all you want. Just be certain to protect yourself from concurrency issues. There are various patterns that can be used for this, let me know if you need any details.

>> If a structure can be shared and updated across plugins, then I quess that is should be synchronized? Say use a hashtable rather than a hashmap?
This is a design preference. I personally never use Hashtable as I favor using Map references with a HashMap implementation. Then when necessary, I just synchronize access to the Map. The short answer though is that yes, you need to synchronize your collections to be safe. Whether you do it yourself or let the platform do it is up to you.

>> Are there ways to serialize access to a shared object? My experience with multithreading is were I either create my own threads and take care of synchronization, or I use ejbs and let the ejb container take care of threading issues.
This isn't a built-in serialization scheme for plugin-to-plugin interaction. Typically, I will use a singleton or statically referenced data and methods. Right now, all plugins share the same classloader with the server itself. This means they all share the same static-scope. While this is changing in the 4.0 version of the server, it will have some backwards compatible workarounds so you can safely use it. You can also use PluginInteropt to have plugins communicate with each other. This works well and even facilitates Java to ActionScript (and vise-versa) interaction, but is a bit heavy-handed for Java when we have more elegent implementations available.

>> Do plug-in setInterval events correspond to Java timer threads?
Yes, the setInterval call simply creates and registeres an underlying Java timer.

>> If multiple plug-ins share access to a file that they append with writeFile, does a plugin have a write lock during the append? I experience this kind of issue with log4j daily appender.
This will depend on the underlying code. If you use various java.io libraries to write to a file in multiple plugins, then it's likely they will conflict with each other unless you manually lock. I don't believe that the built-in plugin helper IO methods protect from this. I don't remember for certain off the top of my head. I will check into this and confirm. This looks like it might be a bug to be corrected. I'll take care of it if needed.

>> Can plug-ins create their own i/o streams?
Yes. Any and all Java APIs + any other APIs you want to use are available to you.

>> If I create a simple standalone http client cla

terryhanks
02-23-2005, 09:55 AM
Michael,

Thanks for your detailed responses to all my questions. I now have a much better understanding of what I can do with Java plug-ins with your game server.

I just received a copy of your Game Design book for Flash MX. I will start reading through it, tonight.

Regards,

Terry

webgeek
02-23-2005, 06:01 PM
Glad to be of help. Was it the MX book or the 2004 book? I believe the MX book is using ElectroServer 2. Big changes since those days. Thanks!

webgeek
02-23-2005, 06:53 PM
Oops, one correction/addition... Anything you want the server to pickup at runtime should be in the plugin folder.