Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Achieve simple async tasks like reading from DB or local file or some local long running process #245

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

Closed
mcaap opened this issue Nov 26, 2020 · 4 comments

Comments

@mcaap
Copy link

mcaap commented Nov 26, 2020

Hi,

Thanks for the nice library. We use it in production!
I have read the literature multiple times, but not sure how to achieve a simple async task.

NginxJavaRequest r = request;
		NginxHttpServerChannel channel = r.hijack(true);
		channel.addListener(channel, new ChannelCloseAdapter<NginxHttpServerChannel>() {
			@Override
			public void onClose(NginxHttpServerChannel serverChannel) throws IOException {
				logger.info("client closed");
			}
		});
		Thread t = new Thread() {
			@Override
			public void run() {
				logger.info("Run thread");
				try {
					Thread.sleep(5000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				logger.info("Done sleeping 5s");
				try {
					channel.sendResponse(new Object[] {200, ArrayMap.create("Content-Type", "text/plain"), "hello"});
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		};
		t.start();

You have mentioned co-routine & async channel/socket as two options.
I dont want to get into co-routine with wave configuration etc.
How do I achieve the above using async channel/socket?

@xfeep
Copy link
Member

xfeep commented Nov 30, 2020

  1. DB

If your DB has async client API it will be easy because you can call channel.sendResponse after you get result from the DB.

If your DB has HTTP or simple socket interface you can use async channel API from nginx-clojure, this is an example to read HTTP content from remote server (https://github.com/nginx-clojure/nginx-clojure/blob/master/test/java/nginx/clojure/net/SimpleHandler4TestNginxClojureAsynChannel.java)

and the doc can be found at #37

Otherwise you need to use thread , e.g, use thread pool created by yourself or use thread pool mode from nginx-clojure.

  1. Local File

It is quite simple because you can return a File object from you response, e.g.

return new Object[] {200, ArrayMap.create("content-type", "text/html"), new File("myfile.txt") }

or

channel.sendResponse(new Object[] {200, ArrayMap.create("Content-Type", "text/plain"), new File("myfile.txt") });

Then nginx will send the static file by non-blocking way.

@mcaap
Copy link
Author

mcaap commented Nov 30, 2020

Thanks. And if I had to run some local shell script as part of the incoming request and send back the response after few seconds, will the channel api be the way to go or thread pool?

@xfeep
Copy link
Member

xfeep commented Nov 30, 2020

@mcaap
There are two kinds of channels: client channel and server channel. A server channel can be used to send message later. A client channel can be used to read/write message from/to the remote server.
A client channel can not be used to work with local shell script. For this case you can use server channel and thread pool created by your code. BTW for this case you can use thread pool mode from nginx-clojure too.

@mcaap
Copy link
Author

mcaap commented Nov 30, 2020

Thank you for patiently answering my noob questions!

@mcaap mcaap closed this as completed Nov 30, 2020
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