View Javadoc

1   /*
2      Licensed under the Apache License, Version 2.0 (the "License");
3      you may not use this file except in compliance with the License.
4      You may obtain a copy of the License at
5   
6        http://www.apache.org/licenses/LICENSE-2.0
7   
8      Unless required by applicable law or agreed to in writing, software
9      distributed under the License is distributed on an "AS IS" BASIS,
10     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11     See the License for the specific language governing permissions and
12     limitations under the License.
13  */
14  package uk.nhs.interoperability.util;
15  
16  import java.util.Queue;
17  import java.util.concurrent.ExecutorService;
18  import java.util.concurrent.Executors;
19  import java.util.concurrent.LinkedBlockingQueue;
20  
21  import uk.nhs.interoperability.infrastructure.ITKMessagingException;
22  import uk.nhs.interoperability.payload.ITKMessage;
23  import uk.nhs.interoperability.source.ITKMessageSender;
24  import uk.nhs.interoperability.source.ITKMessageSenderImpl;
25  
26  /**
27   * The Class MessageQueue.
28   *
29   * @author Michael Odling-Smee
30   * @author Nicholas Jones
31   * @since 0.1
32   */
33  public class MessageQueue implements Runnable {
34  	
35  	/** The executor service. */
36  	private ExecutorService executorService;
37  	
38  	/** The is running. */
39  	private boolean isRunning = true;
40  	
41  	/** The itk message sender. */
42  	private ITKMessageSender itkMessageSender;
43  	
44  	/*
45  	 * Construct an in-memory queue to queue outbound ITK messages 
46  	 */
47  	/** The async processing queue. */
48  	private Queue<ITKMessage> asyncProcessingQueue = new LinkedBlockingQueue<ITKMessage>();
49  	
50  	/**
51  	 * Instantiates a new message queue.
52  	 */
53  	public MessageQueue() {
54  		this.executorService = Executors.newFixedThreadPool(1);
55  		this.executorService.execute(this);
56  		this.itkMessageSender = new ITKMessageSenderImpl();
57  	}
58  
59  	/**
60  	 * Queue.
61  	 *
62  	 * @param request the request
63  	 */
64  	public void queue(ITKMessage request) {
65  		//Queue for asynchronous processing
66  		this.asyncProcessingQueue.add(request);
67  	}
68  
69  	/**
70  	 * Process async message.
71  	 *
72  	 * @param request the request
73  	 */
74  	private void processAsyncMessage(ITKMessage request) {
75  		Logger.debug("Processing queued itk request");
76  		try {
77  			this.itkMessageSender.send(request);
78  		} catch (ITKMessagingException e) {
79  			/*
80  			 * In a real application some more in-depth error
81  			 * handling may occur such as storing the failed message
82  			 * for attention of an administrator, however for this
83  			 * simple application emulator we just log an error
84  			 */
85  			Logger.error("Could not send aysnchronous request", e);
86  		}
87  	}
88  	
89  	/* (non-Javadoc)
90  	 * @see java.lang.Runnable#run()
91  	 */
92  	@Override
93  	public void run() {
94  		while (this.isRunning) {
95  			try {
96  				if (this.asyncProcessingQueue.isEmpty()) {
97  					Thread.sleep(5000);
98  				} else {
99  					this.processAsyncMessage(this.asyncProcessingQueue.poll());
100 				}
101 			} catch (InterruptedException e) {
102 				this.isRunning = false;
103 			}
104 		}
105 	}
106 
107 }