1 22 package org.jboss.resource.adapter.mail.inflow; 23 24 import java.lang.reflect.Method ; 25 import javax.resource.spi.endpoint.MessageEndpointFactory ; 26 import javax.resource.spi.endpoint.MessageEndpoint ; 27 import javax.resource.spi.work.Work ; 28 import javax.mail.Message ; 29 30 import org.jboss.resource.adapter.mail.MailResourceAdapter; 31 import org.jboss.logging.Logger; 32 33 40 public class MailActivation 41 implements Comparable , Work 42 { 43 private static final Logger log = Logger.getLogger(MailActivation.class); 44 47 public static final Method ON_MESSAGE; 48 49 static 50 { 51 try 52 { 53 Class [] sig = {Message .class}; 54 ON_MESSAGE = MailListener.class.getMethod("onMessage", sig); 55 } 56 catch (Exception e) 57 { 58 throw new RuntimeException (e); 59 } 60 } 61 62 63 private boolean released; 64 65 private boolean trace; 66 67 private long nextNewMsgCheckTime; 68 69 protected MailResourceAdapter ra; 70 71 protected MailActivationSpec spec; 72 73 protected MessageEndpointFactory endpointFactory; 74 75 public MailActivation(MailResourceAdapter ra, MessageEndpointFactory endpointFactory, 76 MailActivationSpec spec) 77 { 78 this.ra = ra; 79 this.endpointFactory = endpointFactory; 80 this.spec = spec; 81 this.trace = log.isTraceEnabled(); 82 } 83 84 public long getNextNewMsgCheckTime() 85 { 86 return nextNewMsgCheckTime; 87 } 88 public void updateNextNewMsgCheckTime(long now) 89 { 90 nextNewMsgCheckTime = now + spec.getPollingInterval(); 91 } 92 93 public int compareTo(Object obj) 94 { 95 MailActivation ma = (MailActivation) obj; 96 long compareTo = nextNewMsgCheckTime - ma.getNextNewMsgCheckTime(); 97 return (int) compareTo; 98 } 99 100 public boolean isReleased() 101 { 102 return released; 103 } 104 105 public void release() 107 { 108 released = true; 109 if( trace ) 110 log.trace("released"); 111 } 112 113 public void run() 114 { 115 released = false; 116 if( trace ) 117 log.trace("Begin new msgs check"); 118 try 119 { 120 MailFolder mailFolder = new MailFolder(spec); 121 mailFolder.open(); 122 Message [] msgs = mailFolder.getNewMessages(); 123 for(int n = 0; released == false && n < msgs.length; n ++) 124 { 125 Message msg = msgs[n]; 126 deliverMsg(msg); 127 } 128 mailFolder.close(); 129 } 130 catch (Exception e) 131 { 132 log.error("Failed to execute folder check, spec="+spec); 133 } 134 if( trace ) 135 log.trace("End new msgs check"); 136 } 137 139 private void deliverMsg(Message msg) 140 { 141 MessageEndpoint endpoint = null; 142 try 143 { 144 endpoint = endpointFactory.createEndpoint(null); 145 if (endpoint != null) 146 { 147 if( trace ) 148 log.trace("deliverMsg, msg subject="+msg.getSubject()); 149 MailListener listener = (MailListener) endpoint; 150 listener.onMessage(msg); 151 } 152 } 153 catch (Throwable e) 154 { 155 log.debug("onMessage delivery failure", e); 156 } 157 finally 158 { 159 if (endpoint != null) 160 { 161 endpoint.release(); 162 } 163 } 164 } 165 166 } 167 | Popular Tags |