none
Pull subscription stopped working (suddenly) RRS feed

  • Question

  • //// This issue weirdly solved itself the next day... The same program run perfectly like before...///

     

    Hello,

     

    Today I did some testing with the Java API (ok, a lot of testing :)) and found it worked like a charm. However, after having played with it for a couple of hours, suddenly things stopped working for me.

    I was simply creating a pull subscription to the server and then treating the events. This worked very well at first, but now I cannot seem to get ANY events anymore (I always get back an empty array from subscription.getEvents() ).

     

    Here is my code :

     

    import java.net.URI;
    import java.net.URISyntaxException;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.Iterator;
    import java.util.List;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextArea;
    
    import microsoft.exchange.webservices.data.*;
    import microsoft.exchange.webservices.data.StreamingSubscriptionConnection.INotificationEventDelegate;
    
    /**
     * @author Joshua Wöhle This class creates an instance of the Microsoft Exchange
     * service and adds some easy functionality (subscribing to calendar,
     * etc.) Everything goes step-by-step for easy debugging.
     */
    public class ExchangeSync {
    
    	ExchangeService service = new ExchangeService();
    	ExchangeCredentials credentials;
    
    	Folder calendarFolder;
    	Folder deletedFolder;
    	Folder inboxFolder;
    	
    	PullSubscription subscription;
    	StreamingSubscription streamingSubscription;
    	StreamingSubscriptionConnection streamingConnection;
    	
    	MyEventsDelegate listener = new MyEventsDelegate();
    	
    	ArrayList<String> mailingList = new ArrayList<String>();
    
    	public void setCredentials(String eMail, String password) {
    		this.credentials = new WebCredentials(eMail, password);
    	}
    
    	ExchangeSync() {
    		
    	}
    
    	public void connect(String url) {
    		service.setCredentials(credentials);
    		System.out.println("Credentials set");
    		try {
    			service.setUrl(new URI(url));
    			System.out.println("URL set correctly");
    		} catch (URISyntaxException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    			System.out.println("Something went wrong while setting URL");
    		}
    	}
    
    	public void bindToCalendar() {
    		try {
    			calendarFolder = Folder.bind(service, WellKnownFolderName.Calendar);
    			System.out.println("Successfully subscribed to calendar : "
    					+ calendarFolder.getDisplayName());
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    
    	public void bindToDeletedFolder() {
    		try {
    			deletedFolder = Folder.bind(service,
    					WellKnownFolderName.DeletedItems);
    			System.out.println("Successfully subscribed to deleted Folder : "
    					+ deletedFolder.getDisplayName());
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    	
    	public void bindToInbox() {
    		try {
    			inboxFolder = Folder.bind(service, WellKnownFolderName.Inbox);
    			System.out.println("Successfully subscribed to calendar : "
    					+ inboxFolder.getDisplayName());
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    
    	public void subscribeToPullCalendarEvents() {
    		List<FolderId> folder = new ArrayList<FolderId>();
    		folder.add(calendarFolder.getId());
    		try {
    			System.out.println("Added "+ calendarFolder.getDisplayName() + " to folder list");
    		} catch (ServiceLocalException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		}
    
    		try {
    			subscription = service.subscribeToPullNotifications(folder, 5,
    					null, EventType.NewMail, EventType.Created,
    					EventType.Deleted, EventType.Modified);
    			System.out.println("successfully subscribed to pull notifications");
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    			System.out.println("Something went wront while subscribing to the pull notifications of the calendar");
    		}
    	}
    	
    	/**
    	 * This method is not function properly yet :S
    	 */
    	public void subscribeToStreamingCalendarNotifications() {
    		FolderId folderId = calendarFolder.getId();
    		
    		List folder = new ArrayList<FolderId>();
    		folder.add(folderId);
    		listener.service = this.service;
    		
    		try {
    			streamingSubscription = service.subscribeToStreamingNotifications(folder, EventType.Created, EventType.Deleted, EventType.Modified);
    			System.out.println("Successfully subscribed to streaming notifications.");
    			
    			streamingConnection = new StreamingSubscriptionConnection(service, 30);
    			streamingConnection.addSubscription(streamingSubscription);
    			
    			streamingConnection.addOnNotificationEvent(listener);
    			streamingConnection.addOnDisconnect(listener);
    			streamingConnection.open();
    			Appointment app = new Appointment(service);
    			app.setSubject("this is a subject");
    			app.setStart(new Date());
    			app.setEnd(new Date());
    			app.save();
    			Thread.sleep(20000);
    			streamingConnection.close();
    			System.out.println("end....");
    			
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    	}
    
    	public void printEvents() {
    		try {
    			GetEventsResults events = subscription.getEvents();
    			System.out.println();
    			System.out.println("Successfully found the events : " + events.getItemEvents());
    
    			for (ItemEvent itemEvent : events.getItemEvents()) {
    				if ((itemEvent.getEventType() == EventType.Created)
    						|| (itemEvent.getEventType() == EventType.Modified)
    						|| (itemEvent.getEventType() == EventType.Deleted)) {
    					this.sendMail(itemEvent);
    				} else
    					System.out.println("Something else happened : " + itemEvent.getEventType());
    			}
    
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    
    	public void addToMailingList(String email) {
    		mailingList.add(email);
    	}
    
    	/**
    	 * This method takes an item event and sends it to the configured mailing
    	 * list
    	 * 
    	 * @param itemEvent
    	 */
    	public void sendMail(ItemEvent itemEvent) {
    		String subject = "An event was " + itemEvent.getEventType();
    		String message = "";
    		Appointment appointment;
    		
    		
    		try {
    			appointment = Appointment.bind(service, itemEvent.getItemId());
    			
    			System.out.println("Successfully bound to the item");
    			if (itemEvent.getEventType() == EventType.Created) {
    				message = "An event was created in the Calendar, these are the details : <br>\n"
    						+ "Event subject : "
    						+ appointment.getSubject()
    						+ "<br>\n"
    						+ "Created on : "
    						+ appointment.getDateTimeCreated()
    						+ "<br>\n"
    						+ "Created by : "
    						+ appointment.getLastModifiedName()
    						+ "<br>\n"
    						+ "Start of appointment : "
    						+ appointment.getStart()
    						+ "<br>\n"
    						+ "End of appointment : " + appointment.getEnd();
    			} else if (itemEvent.getEventType() == EventType.Modified) {
    				Iterator<OccurrenceInfo> it = appointment.getModifiedOccurrences().iterator();
    				
    				if(it.hasNext()) {
    					System.out.println(it.next().toString());
    				}
    				message = "An event was modified in the Calendar, these are the details : <br>\n"
    						+ "Event subject : "
    						+ appointment.getSubject()
    						+ "<br>\n"
    						+ "Modified on : "
    						+ appointment.getLastModifiedTime()
    						+ "<br>\n"
    						+ "Modified by : "
    						+ appointment.getLastModifiedName()
    						+ "<br>\n"
    						+ "Start : "
    						+ appointment.getStart()
    						+ "<br>\n" + "End : " + appointment.getEnd();
    			} else
    				System.out.println("Something else");
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    
    		/*JFrame myFrame = new JFrame();
    		JTextArea tarea = new JTextArea();
    		tarea.setText(message);
    		tarea.setColumns(50);
    		tarea.setRows(10);
    		myFrame.getContentPane().add(tarea);
    		myFrame.pack();
    		myFrame.setVisible(true);*/ 
    		
    		System.out.println(subject + "--\n" + message);
    		// this.sendMail(this.mailingList, subject, message);
    	}
    
    	public void sendMail(ArrayList<String> toList, String subject,
    			String message) {
    		try {
    			EmailMessage msg = new EmailMessage(service);
    			System.out.println("Successfully created new mail");
    
    			msg.setSubject(subject);
    			msg.setBody(MessageBody.getMessageBodyFromText(message));
    			for (String to : toList)
    				msg.getToRecipients().add(to);
    			msg.send();
    			System.out.println("Message sent !");
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    
    	}
    
    }
    

    and then I call it in my TesterClass like this :

     

    import java.util.Timer;
    import java.util.TimerTask;
    
    public class TesterClass {
    	
    	public static void main(String[] args) {
    		
    		final ExchangeSync exchange = new ExchangeSync();
    		exchange.setCredentials("XXXXX", "XXXXX");
    		exchange.connect("XXXX");
    		exchange.bindToCalendar();
    		exchange.bindToDeletedFolder();
    		exchange.bindToInbox();
    		exchange.subscribeToPullCalendarEvents();
    		exchange.addToMailingList("XXXXXXX");
    		
    		int delay = 5000;
    		int interval = 10000;
    		
    		Timer timer = new Timer();
    		
    		timer.scheduleAtFixedRate(new TimerTask() {
    
    			@Override
    			public void run() {
    				// TODO Auto-generated method stub
    				exchange.printEvents();
    			}
    		}, delay, interval);
    		
    	}
    	
    }
    

     

    My interest at the moment is in the Pull subscription, so please do not pay attention to the streamnotificationlistener.

     

    If anyone has an idea of what might be going on, thanks for the help :)

    Joshua




    • Edited by Findarato Tuesday, March 29, 2011 5:11 PM resolved
    Monday, March 28, 2011 4:55 PM