Results 1 to 1 of 1
  1. #1
    skuskusas is offline Member
    Join Date
    Aug 2012
    Posts
    5
    Rep Power
    0

    Default Run app as windows service.

    Hi i have problem with run windows service i using Procrun. Service dont start.
    My code is
    Java Code:
    package mm;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.ArrayList;
    import java.util.Properties;
    
    import com.caen.RFIDLibrary.CAENRFIDException;
    import com.caen.RFIDLibrary.CAENRFIDLogicalSource;
    import com.caen.RFIDLibrary.CAENRFIDPort;
    import com.caen.RFIDLibrary.CAENRFIDProtocol;
    import com.caen.RFIDLibrary.CAENRFIDReader;
    import com.caen.RFIDLibrary.CAENRFIDReaderInfo;
    import com.caen.RFIDLibrary.CAENRFIDTag;
    
    
    
    
    public class App extends Thread {
    	
    	CAENRFIDReader reader; 
    	CAENRFIDPort port;
    	CAENRFIDLogicalSource MySource;
    
    	String gatesID;
    	String readerIP;
    	String sendToURL;
    	int antPower = 75;
    
    	final static int STATE_IDLE = 8191;
    	final static int STATE_A = 8189;
    	final static int STATE_B = 8187;
    	final static int STATE_AB = 8185;
    
    	final static int ENTRY_POINT_NONE = 0;
    	final static int ENTRY_POINT_A = 1;
    	final static int ENTRY_POINT_B = 2;
    
    	boolean state_a_active = false;
    	boolean state_b_active = false;
    	boolean state_ab_active = false;
    	int entry_point = 0;
    	int io;
    	long firstReadTimeMillis;
    	long currentTimeMillis;
    	long sessionTimeOut = 3000;
    	int swapDirections = 0;
    	int debug_mode = 0;
    	String tagIdStart = "";
    
    	ArrayList<String> tagslist = new ArrayList<String>();
    	
    	App() {
    	//	super();
    		System.out.println("mm");
    		
    		Properties prop = new Properties();
    		try {
    			prop.load(new FileInputStream("config.properties"));
    			gatesID = prop.getProperty("gates_id");
    			readerIP = prop.getProperty("reader_ip");
    			sendToURL = prop.getProperty("sendToURL");
    			antPower = Integer.valueOf(prop.getProperty("power"));
    			swapDirections = Integer.valueOf(prop.getProperty("swapDirections"));
    			debug_mode = Integer.valueOf(prop.getProperty("debug_mode"));
    			tagIdStart= prop.getProperty("tag_id_start");
    	 	} catch (IOException ex) {
    	 		ex.printStackTrace();
    	    }
    		
    		port = CAENRFIDPort.CAENRFID_TCP;
    		reader = new CAENRFIDReader();
    		try {
    			reader.Connect(port, readerIP);
    			reader.SetPower(antPower);
    			CAENRFIDReaderInfo info = reader.GetReaderInfo();
    			reader.GetFirmwareRelease();
                reader.SetProtocol(CAENRFIDProtocol.CAENRFID_EPC_C1G2);
                System.out.println(info.GetModel());
                //System.out.println(readerIP);
                MySource = reader.GetSource("Source_0");
                MySource.AddReadPoint("Ant0");
                MySource.AddReadPoint("Ant1");
                MySource.AddReadPoint("Ant2");
                MySource.AddReadPoint("Ant3");
                MySource.SetQ_EPC_C1G2(2);
    		} catch (CAENRFIDException e) {
    			e.printStackTrace();
    		}
    	
    	}
    	public void run() {
    		System.out.println("63 eilute on");
    		while (true) {
    			try {
    				io = reader.GetIO();
    				if (STATE_IDLE != io)  {
    					 switch (io) {
    			            case STATE_A: System.out.println("A");
    			            	if (entry_point == ENTRY_POINT_NONE){
    			            		entry_point = ENTRY_POINT_A;
    			            		firstReadTimeMillis = System.currentTimeMillis();
    			            	}
    	                    break;
    	                    
    			            case STATE_B: System.out.println("B");
    			            	if (entry_point == ENTRY_POINT_NONE){
    			            		entry_point = ENTRY_POINT_B;
    			            		firstReadTimeMillis = System.currentTimeMillis();
    			            	}
                        	break;
    	                    
    			            case STATE_AB: System.out.println("AB");
    			            	state_ab_active = true;
    			            	scanTags();
    	                    	break;
    	                    
    	                    default:	
    	                    	System.out.println(io);
    	                    	break;
    				 }
    					
    	
    				} else {
    					if (entry_point != ENTRY_POINT_NONE && state_ab_active) {
    						System.out.print("Entry point: ");
    						System.out.println(entry_point);
    						System.out.print("Tags: ");
    						System.out.println(tagslist.toString());
    						URL url;
    						InputStream is = null;
    						DataInputStream dis;
    						String urlString;
    						String s;
    						try {
    							String tagsString = tagslist.toString();
    							tagsString = tagsString.replaceAll("\\s","");
    							tagsString = tagsString.replaceAll("\\[","");
    							tagsString = tagsString.replaceAll("\\]","");
    							
    							urlString = sendToURL + "?tags=" + tagsString  + "&d=" + prepareDirection(entry_point)
    									+ "&gates=" + gatesID;
    							System.out.println(urlString);
    							url = new URL(urlString);
    							
    							if (debug_mode == 0) {
    								HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
    							    System.out.println("Response code is " + httpCon.getResponseCode());
    							}
    							
    						} catch (MalformedURLException e) {
    							System.out.println("MalformedURLException");
    							e.printStackTrace();
    						} catch (IOException e) {
    							System.out.println("IOException");
    							e.printStackTrace();
    						} finally {
    
    						}
    
    						entry_point = ENTRY_POINT_NONE;
    						state_ab_active = false;
    						clearTags();
    						sleep(5000);
    					}
    					
    					if (entry_point != ENTRY_POINT_NONE) {
    						currentTimeMillis = System.currentTimeMillis();
    						if (currentTimeMillis - firstReadTimeMillis > sessionTimeOut) {
    							entry_point = ENTRY_POINT_NONE;
    							state_ab_active = false;
    							clearTags();
    							System.out.print("Session timeout");
    						}
    					}
    				}
    				sleep(10);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			} catch (CAENRFIDException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
        }
    	
    	
    	public void scanTags() {
    		CAENRFIDTag[] myTags = null;
    		try {
    			myTags = MySource.InventoryTag();
    		} catch (CAENRFIDException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
            if (myTags != null) { 
                if (myTags.length > 0) {
                	for (int i = 0; i < myTags.length; i++) {
                		CAENRFIDTag tag = myTags[i];
                		//byte[] id = tag.GetId();
                		//if (id[0] != 48) {
                			String tagId = new String(tag.GetId());
                			//tagId = tagId.replaceAll("\\s","");
                			tagId = tagId.replaceAll("[^a-zA-Z 0-9]+","");
                			if (tagId.length()>0)
                			//if (tagId.startsWith(tagIdStart))
    		            		if (!tagslist.contains(tagId))
    		            			tagslist.add(tagId);
                		//}
            		}
                }
            }
    	}
    	
    	public void clearTags() {
    		tagslist.clear();
    	}
    	
    	public int prepareDirection(int direction) {
    		if (swapDirections > 0) {
    			int swapedDirection = 0;
    			if (direction == ENTRY_POINT_A) swapedDirection = ENTRY_POINT_B;
    			if (direction == ENTRY_POINT_B) swapedDirection = ENTRY_POINT_A;
    			return swapedDirection;
    		} else return direction;
    	}
    	
    		private static boolean stop = false;
    
    		
    		public static void start(String[] args) {
    			System.out.println("Service started");
    			App app = new App();
    			app.setDaemon(true);
    			app.run();
    			//readThread.setDaemon(true);
    			//readThread.start();
    			while (!stop) {
    				try {
    					Thread.sleep(1000);
    				} catch (InterruptedException e) {
    				}
    				System.out.println("running");
    			}
    			System.out.println("Service finished");
    		}
    
    		
    			
    		
    		public static void stop(String[] args) {
    			System.out.println("stop");
    			stop = true;
    		}
    
    		public static void main(String[] args) {
    			System.out.println(args);
    			if ("start".equals(args[0])) {
    				System.out.println("args0");
    				start(args);
    			} else if ("stop".equals(args[0])) {
    				System.out.println("args1");
    				stop(args);
    			}
    		}
    
    	}
    Last edited by skuskusas; 11-05-2012 at 01:57 PM. Reason: Change code

Similar Threads

  1. Replies: 0
    Last Post: 07-05-2011, 08:41 AM
  2. DayTime Service (Use Java in Windows and Linux)
    By Harris in forum New To Java
    Replies: 1
    Last Post: 12-07-2010, 06:08 PM
  3. java application as windows service
    By gerard kowara in forum JDBC
    Replies: 1
    Last Post: 11-05-2010, 09:08 AM
  4. Create Windows Service From Jar
    By appdevman in forum Advanced Java
    Replies: 1
    Last Post: 06-24-2010, 02:17 AM
  5. run a batch file as windows nt service
    By radhika123 in forum New To Java
    Replies: 0
    Last Post: 04-04-2008, 01:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •