Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to use jactor #14

Closed
cp149 opened this issue Feb 29, 2012 · 17 comments
Closed

how to use jactor #14

cp149 opened this issue Feb 29, 2012 · 17 comments

Comments

@cp149
Copy link

cp149 commented Feb 29, 2012

hi,laforge49
I'm new to Jactor,I got code,and run the test ,It is fast.I read your document,but can not find a right way to use in my web app.
my web app is very simple,user input a task,every task run 10-100 second.I write a unit test such as blow
public class TaskTest {
//queuque to save taskids
LinkedBlockingQueue taskqueque=new LinkedBlockingQueue();
//flag,stop taskserver
boolean stop=false;

private ExecutorService taskExecutor= Executors.newFixedThreadPool(5);

@Before
public void setUp() throws Exception {
    //create 5 taks server
    for (int i = 0; i < 5; i++) {
        taskExecutor.submit( new Taskserver());         
    }
}

@Test
public void test() {
    int times=new Random().nextInt(50);
    System.out.println("run "+times+" tasks");
    for (int i = 0; i <times; i++) {
        //add new task to queque
        taskqueque.add(String.valueOf(i));
    }
    while(!taskqueque.isEmpty());
    stop=true;
    System.out.println("test over");
}
class Taskserver implements Runnable{


    @Override
    public void run() {
        while(!stop){
            try {
                String taskid=taskqueque.take();
                //sleep some times,simulate db read write 
                Thread.sleep(1000*new Random().nextInt(10));
                System.out.println("work over task "+taskid+" at thread"+Thread.currentThread().getName() );
            } catch (InterruptedException e) {

            }
        }

    }
}

}

can you give me a sample how to use jactor in this class? thank you very much.

@laforge49 laforge49 reopened this Feb 29, 2012
@laforge49
Copy link
Owner

The example I gave above uses 10 threads, so it can process 10 requests at a time.

The driver creates n workers, each with its own asynchronous mailbox, and sends each one a request. The driver exits when it has received responses for all the requests sent. In production code, you would also want to use an exception handler so you can treat any raised exceptions as responses as well.

@laforge49
Copy link
Owner

I should perhaps clarify a bit. Threads are expensive, so to service lots of simultaneous users you should not dedicate a separate thread to each request.

Now a lot of modern web servers, Jetty being just one, support continuations. Integrating web server continuations with actors is one way to do things while minimizing thread usage--actors being light-weight threads.

@cp149
Copy link
Author

cp149 commented Mar 1, 2012

thank you very much,I'll try it

@laforge49
Copy link
Owner

Keep in mind that, except when doing I/O, actors should never block. And there are no locks anywhere.

@cp149
Copy link
Author

cp149 commented Mar 3, 2012

hi, laforge49.
most of my tasks does few I/O,but sometimes it need write file or connect to other server.when this happen,It block tasks in mailbox,it's wast user's time to wait.

@laforge49
Copy link
Owner

Yes, this is important.

I/O can be done is a separate actor, which has its own dedicated asynchronous mailbox. Actors which then send a request to this I/O actor then will not get a response until the I/O completes, but are not blocked in that they can still process other requests.

The other thing that you need to realize is that when an actor is processing a request, it can send a request to that I/O actor and still do other things, like sending a response to the request they are processing. But then what do you do when you do get that response from the I/O actor? Remember that for each request received, an actor can send only one response. So it gets tricky.

So it comes down to your top-level design. You get a request from an actor and you want to (1) initiate some activity and also (2) respond to the user. So what do you then do when that (asynchronous) activity completes? Do you want to (a) update some state which will effect some future request by the user? (AJAX-like) Or do you want to send an update to the user? (CometD-like?)

Bill

@cp149
Copy link
Author

cp149 commented Mar 4, 2012

Ok .
let me do some change in design,thank you very much.

@laforge49 laforge49 reopened this Mar 4, 2012
@laforge49
Copy link
Owner

Lets leave this issue open for now. I think this discussion is valuable and would be visible to a wider community if left open.

@cp149
Copy link
Author

cp149 commented Mar 28, 2012

hi, laforge49
I'm back.after three week's hard work.I import Jactor into my project,and it work great.
My design is
1、user request is send to a topactor via a asynchronous mailbox
2、topactor get service actor via commons pool,then send request to this service actor
3、when service actor complete request,then it return himself back to pool.
in this way.I improve throughput.thank you for your great job

@laforge49
Copy link
Owner

I do hope the changes I've been making do not cause you problems. My current plan is to drop JBActor, Component and all their subclasses, leaving only JLPCActor and its subclasses. But before I do this I need to rewrite a number of components as actors, as components will no longer be supported.

What has happened is that JLPCActors have become very light-weight, allowing the creation of more than a billion of them each second, as well as becoming more powerful. By dropping JBActor, I lose composition but nothing else. JBActors (and the subclass, JCActor) are quite slow in comparison and add a lot of complexity to the project.

@cp149
Copy link
Author

cp149 commented Mar 28, 2012

I just use JLPCActors and JAEvent,hehe

@cp149
Copy link
Author

cp149 commented Apr 6, 2013

I write an appender for logback with jactor ,It's here https://github.com/cp149/jactor-logger

@laforge49
Copy link
Owner

Very nice! I was thinking that I would have to write that myself, so I am
especially happy to see this.

I've placed a link to your project here:
http://jactorconsulting.com/product/jactor/

Meanwhile we have been working on the next generation of Jactor, which you
might want to look at:

API: https://github.com/laforge49/PActor
Implementation: https://github.com/laforge49/PAMailbox (Includes the PActor
tests.)
utilities: https://github.com/laforge49/PAUtil (includes more tests.)
benchmarks: https://github.com/skunkiferous/PingPong

Here's some comparisons with other actor frameworks:
http://skunkiferous.github.io/PingPong/

Everything works, but we are still adding additional speedups to the
implementation. I'm also working on migrating JID to work with PActor.

For the moment, there are no docs, except for the javadocs in PActor. And
nothing has been released yet. Indeed, I still need to write the javadocs
for PAUtil!

But this time around, I'm not working alone.And the code is ever so much
cleaner. Monster has been a major contributor and Vicky has been
contributing as well. But it is an open project and, if you are interested,
there is no shortage of work. :-)

Bill

On Sat, Apr 6, 2013 at 11:06 AM, cp149 notifications@github.com wrote:

I write an appender for logback with jactor ,It's here
https://github.com/cp149/jactor-logger


Reply to this email directly or view it on GitHubhttps://github.com//issues/14#issuecomment-15991241
.

@laforge49
Copy link
Owner

cp149,

I dug into your code a bit and I think it is a great application for
JActor. I was actually thinking of something else though...

The logback project has some known issues regarding closing, i.e. if it is
not shutdown properly then log messages get lost, which is the worst
possible behavior for a logger! So I'm generally suspicious of their
transport. I was thinking it would be nice to be able to use JASocket as
the transport instead.

Of course, I understand from Monster that JASocket also has an issue
because it is not using direct memory, and the default behavior of the JVM
is flawed:
https://groups.google.com/forum/?fromgroups=#!topic/asynchbase/xFvHuniLI1c
(Netty
apparently gets it right.)

Bill

On Sat, Apr 6, 2013 at 12:19 PM, William la Forge laforge49@gmail.comwrote:

Very nice! I was thinking that I would have to write that myself, so I am
especially happy to see this.

I've placed a link to your project here:
http://jactorconsulting.com/product/jactor/

Meanwhile we have been working on the next generation of Jactor, which you
might want to look at:

API: https://github.com/laforge49/PActor
Implementation: https://github.com/laforge49/PAMailbox (Includes the
PActor tests.)
utilities: https://github.com/laforge49/PAUtil (includes more tests.)
benchmarks: https://github.com/skunkiferous/PingPong

Here's some comparisons with other actor frameworks:
http://skunkiferous.github.io/PingPong/

Everything works, but we are still adding additional speedups to the
implementation. I'm also working on migrating JID to work with PActor.

For the moment, there are no docs, except for the javadocs in PActor. And
nothing has been released yet. Indeed, I still need to write the javadocs
for PAUtil!

But this time around, I'm not working alone.And the code is ever so much
cleaner. Monster has been a major contributor and Vicky has been
contributing as well. But it is an open project and, if you are interested,
there is no shortage of work. :-)

Bill

On Sat, Apr 6, 2013 at 11:06 AM, cp149 notifications@github.com wrote:

I write an appender for logback with jactor ,It's here
https://github.com/cp149/jactor-logger


Reply to this email directly or view it on GitHubhttps://github.com//issues/14#issuecomment-15991241
.

@cp149
Copy link
Author

cp149 commented Apr 7, 2013

Glad to see you have start next generation of Jactor. It is a great project,very fast and stable.It has run with my project in the past year.
I'm very interesting to contribute to Pactor,but I'm a newbie to github,I need to learn how to work with others in github. Let me start it from reading Pactor,:)

@laforge49
Copy link
Owner

Many thanks for the kind words.

Consider also joining our group:
https://groups.google.com/forum/?hl=en&fromgroups#!forum/agilewikidevelopers
(If
you don't want the email, you can always disable it.)

Bill

On Sun, Apr 7, 2013 at 8:30 AM, champion notifications@github.com wrote:

Glad to see you have start next generation of Jactor. It is a great
project,very fast and stable.It has run with my project in the past year.
I'm very interesting to contribute to Pactor,but I'm a newbie to github,I
need to learn how to work with others in github. Let me start it from
reading Pactor,:)


Reply to this email directly or view it on GitHubhttps://github.com//issues/14#issuecomment-16008466
.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants