View Javadoc
1   /*
2    The contents of this file are subject to the Mozilla Public License Version 1.1
3    (the "License"); you may not use this file except in compliance with the License.
4    You may obtain a copy of the License at http://www.mozilla.org/MPL/
5    Software distributed under the License is distributed on an "AS IS" basis,
6    WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
7    specific language governing rights and limitations under the License.
8   
9    The Original Code is "TheHapiContext.java ".  Description:
10   "Exception containing a Location and Error Code"
11  
12   The Initial Developer of the Original Code is University Health Network. Copyright (C)
13   2013.  All Rights Reserved.
14  
15   Contributor(s): ______________________________________.
16  
17   Alternatively, the contents of this file may be used under the terms of the
18   GNU General Public License (the "GPL"), in which case the provisions of the GPL are
19   applicable instead of those above.  If you wish to allow use of your version of this
20   file only under the terms of the GPL and not to allow others to use your version
21   of this file under the MPL, indicate your decision by deleting  the provisions above
22   and replace  them with the notice and other provisions required by the GPL License.
23   If you do not delete the provisions above, a recipient may use your version of
24   this file under either the MPL or the GPL.
25   */
26  
27  package ca.uhn.hl7v2.examples;
28  
29  import java.util.concurrent.Executors;
30  
31  import ca.uhn.hl7v2.DefaultHapiContext;
32  import ca.uhn.hl7v2.HapiContext;
33  import ca.uhn.hl7v2.app.HL7Service;
34  import ca.uhn.hl7v2.validation.builder.support.DefaultValidationBuilder;
35  import ca.uhn.hl7v2.validation.builder.support.NoValidationBuilder;
36  
37  public class TheHapiContext {
38  
39  	/**
40  	 * Introducing the HAPI context
41  	 */
42  	@SuppressWarnings("unused")
43  	public static void main(String[] args) {
44  		
45  		/*
46  		 * Since HAPI 2.1, before using most parts of HAPI you sould create a context
47  		 * object. The HAPI Context acts as a central place for configuration and
48  		 * instantiation of HAPI objects.
49  		 * 
50  		 * Contructing one is simple:
51  		 */
52  		HapiContext ctx = new DefaultHapiContext();
53  		
54  		/*
55  		 * The context is the "one stop shop" for configuring and instantiating
56  		 * HAPI's rich toolset. The other example pages will explain in detail 
57  		 * how to use these features, so here we'll just focus on the HAPI context 
58  		 * itself. 
59  		 */
60  		
61  		/*
62  		 * As a simple example, let's configure this context to have message 
63  		 * validation disabled. 
64  		 */
65  		ctx.setValidationRuleBuilder(new NoValidationBuilder());
66  		
67  		/*
68  		 * We can now use this context as much as we want, and it's configuration
69  		 * will be applied every time.
70  		 * 
71  		 * Here we create two servers on ports 8080 and 8081.
72  		 */
73  		HL7Service server1 = ctx.newServer(8080, false);
74  		HL7Service server2 = ctx.newServer(8081, false);
75  		
76  		/*
77  		 * If we want these servers to have different configurations, we just
78  		 * use a separate context for each server. 
79  		 */
80  		HapiContext ctx1 = new DefaultHapiContext();
81  		ctx1.setValidationRuleBuilder(new NoValidationBuilder());
82  		server1 = ctx1.newServer(8080, false);
83  		
84  		HapiContext ctx2 = new DefaultHapiContext();
85  		ctx2.setValidationRuleBuilder(new DefaultValidationBuilder());
86  		server2 = ctx2.newServer(8081, false);
87  
88  		/*
89  		 * Many of HAPI's internal tasks rely on background threads to
90  		 * do work (in particular, HL7 servers require several execution
91  		 * threads). HAPI uses the "java.util.concurrent.ExecutorService"
92  		 * facility to schedule background work. 
93  		 * 
94  		 * By default all instances of DefaultHapiContext will share a 
95  		 * single thread pool executor instance. This should be fine for 
96  		 * most use cases as the shared shared executor will grow as needed
97  		 * and close completely when it isn't.
98  		 * 
99  		 * You can override this if you want different behaviour. 
100 		 */
101 		ctx = new DefaultHapiContext();
102 		ctx.setExecutorService(Executors.newCachedThreadPool());
103 		
104 		/*
105 		 * See the rest of the examples for more information on how to 
106 		 * use the context:
107 		 * http://hl7api.sourceforge.net/devbyexample.html
108 		 */
109 
110 
111 	}
112 
113 }