This week's book giveaway is in the Java 9 forum.
We're giving away four copies of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications and have Sander Mak & Paul Bakker on-line!
See this thread for details.
Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Queue of incoming messages  RSS feed

 
Hill Williams
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody!
I have a task to implement Http Server which can receive message and send it to another adress. But i need wait 6 sec. before send every message to remote server.
So i decided to make a queue and put all incoming messages in it and then every 6 sec take message from this queue and send it.
But i don't know how implement the scheduled queue. Maybe someone can give some advice?


this is example of my code

public void handle(final HttpExchange t) throws IOException, MalformedURLException {
//task to send message.
TimerTask getMsgTask = new TimerTask() {
@Override
public void run() {
if(!qu.isEmpty()){
buffer = qu.remove();
System.out.println(buffer);
//buffer = msg.sendFile(conParam,buffer);//send to remote host and remove message from queue
sendMessageResp(buffer, t);
}
}
};

System.out.println("Receving...");
buffer = receiveMessageReq(t); // receive incoming message
conParam = config.getURL(); // connect params read from file
qu.add(buffer); // add into the queue

timer.schedule(getMsgTask,6000); //execute the sending task
}

//Receive request
public byte[] receiveMessageReq(HttpExchange t) throws IOException{
InputStream inStream = new BufferedInputStream( (InputStream) t.getRequestBody());
byte[] inBuffer = new byte[inStream.available()];
try {
inStream.read(inBuffer);

} catch (IOException e) {
System.out.println(e.fillInStackTrace());
}
return inBuffer;
}

// response
public void sendMessageResp(byte[] buffer, HttpExchange t){
try {
t.sendResponseHeaders(200,0);
OutputStream os = t.getResponseBody();
os.write(buffer);
os.close();
} catch (Exception ex) {
System.out.println(ex.fillInStackTrace());
}
}

public void addInBuffer(byte[] inBuf){
buffer = inBuf;
}

public byte[] getBuffer(){
return buffer;
}
Sanx =)
 
Henry Wong
author
Sheriff
Posts: 23289
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hill Williams wrote: But i need wait 6 sec. before send every message to remote server.
So i decided to make a queue and put all incoming messages in it and then every 6 sec take message from this queue and send it.
But i don't know how implement the scheduled queue. Maybe someone can give some advice?



One option is to use the java.util.concurrent.DelayQueue and java.util.concurrent.Delayed classes. With it, you can put a six second delay on the elements before it can be removed from the queue. There is no need to wait six seconds before reading the queue -- just read the queue, and the caller will block until the elements are ready to be removed.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!