How to Synchronize Red5 NetConnection Calls - java

I am developing a online game using red5 and flex. using RTMP connection. I used only netConnection.call. my issue is the red5 calls are not coming synchronized manor. some calls are coming to client suddenly some calls are taking time. I want to make this calls reach to client side in order. please help me any one...

Followings are my opinions, I'm sure there are far better ways to do this.
Write a class that is responsible from NetConnection.call execution. in this class, make sure that no call is made before previous one is completed. It ensures the order, but slows execution.
Write a class such that: There should be a data structure, maybe an array in its simplest form. Array contains objects that holds call order, callback function and result returned from server. When you call a method, add those calls into the array in calling order. When you receive a result from server, check the array. if previous calls are not returned yet, store them in array. If there are no previous calls pending, call your callback function any functions "called later but finished earlier that this" and remove that item from your array.
But, (there is always a but in red5), if your application needs some result in order, maybe you should consider your architecture. Most of the time, a carefully thought event handling mechanism removes the need or ordered results.

Red5 offers two application adapters which support synchronized and multithreaded access. To use them, simply extend org.red5.server.adapter.ApplicationAdapter for sync or org.red5.server.adapter.MultiThreadedApplicationAdapter in your application.

Related

Is Session.sendToTarget() thread-safe?

I am trying to integrate QFJ into a single-threaded application. At first I was trying to utilize QFJ with my own TCP layer, but I haven't been able to work that out. Now I am just trying to integrate an initiator. Based on my research into QFJ, I would think the overall design should be as follows:
The application will no longer be single-threaded, since the QFJ initiator will create threads, so some synchronization is needed.
Here I am using an SocketInitiator (I only handle a single FIX session), but I would expect a similar setup should I go for the threaded version later on.
There are 2 aspects to the integration of the initiator into my application:
Receiving side (fromApp callback): I believe this is straightforward, I simply push messages to a thread-safe queue consumed by my MainProcessThread.
Sending side: I'm struggling to find documentation on this front. How should I handle synchronization? Is it safe to call Session.sendToTarget() from the MainProcessThread? Or is there some synchronization I need to put in place?
As Michael already said, it is perfectly safe to call Session.sendToTarget() from multiple threads, even concurrently. But as far as I see it you only utilize one thread anyway (MainProcessThread).
The relevant part of the Session class is in method sendRaw():
private boolean sendRaw(Message message, int num) {
// sequence number must be locked until application
// callback returns since it may be effectively rolled
// back if the callback fails.
state.lockSenderMsgSeqNum();
try {
.... some logic here
} finally {
state.unlockSenderMsgSeqNum();
}
Other points:
Here I am using an SocketInitiator (I only handle a single FIX session), but I would expect a similar setup should I go for the threaded version later on.
Will you always use only one Session? If yes, then there is no use in utilizing the ThreadedSocketInitiator since all it does is creating a thread per Session.
The application will no longer be single threaded, since the QFJ initiator will create threads
As already stated here Use own TCP layer implementation with QuickFIX/J you could try passing an ExecutorFactory. But this might not be applicable to your specific use case.

Execute the method after previous method finishes

I am making an android app in which I am fetching data from internet and storing it in a ArrayList with custom adapter. Fetching data takes time and in that time next function runs on its own. I only want the next function to run when data is completely fetched. What can I do? I think it has to do something with threads kindly explain what threads are and how can we use them?
Let's say there are 2 functions
Function A
Function B
I only want the function B to run when function A has completed its task. is there anyway to do that?
There are lots of resources available online where you can obtain information on Threads in Java.
I highly recommend the official Java Documentation.
This Introduction isn't half bad either.
As for obtaining information in one method and then waiting until it is done to run the next, as #cHao said, just call the methods sequentially like this
A();
B();
Unless you already have multiple threads set up in your code, this should work just fine.

Time Based Streaming

I am trying to figure out how to get time-based streaming but on an infinite stream. The reason is pretty simple: Web Service call latency results per unit time.
But, that would mean I would have to terminate the stream (as I currently understand it) and that's not what I want.
In words: If 10 WS calls came in during a 1 minute interval, I want a list/stream of their latency results (in order) passed to stream processing. But obviously, I hope to get more WS calls at which time I would want to invoke the processors again.
I could totally be misunderstanding this. I had thought of using Collectors.groupBy(x -> someTimeGrouping) (so all calls are grouped by whatever measurement interval I chose. But then no code will be aware of this until I call a closing function as which point the monitoring process is done.
Just trying to learn java 8 through application to previous code
By definition and construction a stream can only be consumed once, so if you send your results to an inifinite streams, you will not be able to access them more than once. Based on your description, it looks like it would make more sense to store the latency results in a collection, say an ArrayList, and when you need to analyse the data use the stream functionality to group them.

Write to GAE datastore asynchronously

In my Java app, sometimes my users do some work that requires a datastore write, but I don't want to keep the user waiting while the datastore is writing. I want to immediately return a response to the user while the data is stored in the background.
It seems fairly clear that I could do this by using GAE task queues, enqueueing a task to store the data. But I also see that there's an Async datastore API, which seems like it would be much easier than dealing with task queues.
Can I just call AsyncDatastoreService.put() and then return from my servlet? Will that API store my data without keeping my users waiting?
I think you are right that the Async calls seem easier. However, the docs for AsyncDatastore mention one caveat that you should consider:
Note: Exceptions are not thrown until you call the get() method. Calling this method allows you to verify that the asynchronous operation succeeded.
The "get" in that note is being called on the Future object returned by the async call. If you just return from your servlet without ever calling get on the Future object, you might not know for sure whether your put() worked.
With a queued task, you can handle the error cases more explicitly, or just rely on the automatic retries. If all you want to queue is datastore puts, you should be able to create (or find) a utility class that does most of the work for you.
Unfortunately, there aren't any really good solutions here. You can enqueue a task, but there's several big problems with that:
Task payloads are limited in size, and that size is smaller than the entity size limit.
Writing a record to the datastore is actually pretty fast, in wall-clock time. A significant part of the cost, too, is serializing the data, which you have to do to add it to the task queue anyway.
By using the task queue, you're creating more eventual consistency - the user may come back and not see their changes applied, because the task has not yet executed. You may also be introducing transaction issues - how do you handle concurrent updates?
If something fails, it could take an arbitrarily long time to apply the user's updates. In such situations, it probably would have been better to simply return an error to the user.
My recommendation would be to use the async API where possible, but to always write to the datastore directly. Note that you need to wait on all your outstanding API calls, as Peter points out, or you won't know if they failed - and if you don't wait on them, the app server will, before returning a response to the user.
If all you need is for the user to have a responsive interface while stuff churns in the back on the db, all you have to do is make an asynchronous call at the client level, aka do some ajax that sends the db write request, changes imemdiatelly the users display, and then upon an ajax request callback update the view with whatever is it you wish.
You can easily add GWT support to you GAE project (either via eclipse plugin or maven gae plugin) and have the time of your life doing asynchronous stuff.

Syncronized methods In MultiThreadedApplicationAdapter

I am using red5 for mmo , and I am in trouble with syncronized methods , the methods which i used in MultiThreadedApplicationAdapter always blocking next request. Is it normal ? Because i am doing some database operations in these methods and this syncronized block is making my performance very poor. I decided to use quartz jobs to overcome this stuation, how ever this time clustered topolgy is making me confused . Could you please help me , is it a common solution to use quartz for this problem , is there any body to give me a smarter advice
Thank you
I want to make an addition to clear my question
After extending MultiThreadedApplicationAdapter and create my overriden class , I implemented
public boolean connect(IConnection conn, IScope scope, Object[] params) {
function and in this function I want to set user status Online (As you can see there is not any syncronized literal in this function however it is acting as there is.)
And I want to take user entity from database and then set online status and then save it back
In this part even if I dont use syncronized literal , next coming client is waiting previous completed. I feel like I must create another job with Quartz and make database operations in that thread however I am not sure this decrease my performance. Is there any other way to prevent this block , this seems to be a Red5 limitation ??
This is also mentioned in a blog http://ria101.wordpress.com/2010/03/09/red5-cabin-fever-advanced-scope-and-room-management/
Only one thread can invoke synchronized method of an object, an quartz won't change it. And from your post doesn't seem what you want to achieve.

Categories

Resources