View Javadoc

1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.ksb.messaging;
17  
18  import java.sql.Timestamp;
19  
20  import javax.persistence.Column;
21  import javax.persistence.Entity;
22  import javax.persistence.GeneratedValue;
23  import javax.persistence.Id;
24  import javax.persistence.NamedQueries;
25  import javax.persistence.NamedQuery;
26  import javax.persistence.Table;
27  import javax.persistence.Transient;
28  import javax.persistence.Version;
29  
30  import org.hibernate.annotations.GenericGenerator;
31  import org.hibernate.annotations.Parameter;
32  import org.kuali.rice.core.api.config.CoreConfigHelper;
33  import org.kuali.rice.core.api.util.RiceUtilities;
34  import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
35  import org.kuali.rice.ksb.api.bus.ServiceConfiguration;
36  import org.kuali.rice.ksb.api.messaging.AsynchronousCall;
37  import org.kuali.rice.ksb.service.KSBServiceLocator;
38  import org.kuali.rice.ksb.util.KSBConstants;
39  
40  /**
41   * A message which has been persisted to the data store.
42   *
43   * @author Kuali Rice Team (rice.collab@kuali.org)
44   */
45  @Entity
46  @Table(name="KRSB_MSG_QUE_T")
47  //@Sequence(name="KRSB_MSG_QUE_S", property="routeQueueId")
48  @NamedQueries({
49    @NamedQuery(name="PersistedMessage.FindAll", query="select pm from PersistedMessageBO pm"),
50    @NamedQuery(name="PersistedMessage.FindByServiceName", query="select pm from PersistedMessage pm where pm.serviceName = :serviceName and pm.methodName = :methodName"),
51    @NamedQuery(name="PersistedMessage.GetNextDocuments", query="select pm from PersistedMessage pm where pm.applicationId = :applicationId and pm.queueStatus <> :queueStatus and pm.ipNumber = :ipNumber order by queuePriority asc, routeQueueId asc, queueDate asc")
52  })
53  public class PersistedMessageBO implements PersistedMessage {
54  
55  	private static final long serialVersionUID = -7047766894738304195L;
56  
57  	@Id
58  	@GeneratedValue(generator="KRSB_MSG_QUE_S")
59  	@GenericGenerator(name="KRSB_MSG_QUE_S",strategy="org.hibernate.id.enhanced.SequenceStyleGenerator",parameters={
60  			@Parameter(name="sequence_name",value="KRSB_MSG_QUE_S"),
61  			@Parameter(name="value_column",value="id")
62  	})
63  	@Column(name="MSG_QUE_ID")
64  	private Long routeQueueId;
65  	@Column(name="PRIO")
66  	private Integer queuePriority;
67  	@Column(name="STAT_CD")
68  	private String queueStatus;
69  	@Column(name="DT")
70  	private Timestamp queueDate;
71  	@Column(name="EXP_DT")
72  	private Timestamp expirationDate;
73  	@Column(name="RTRY_CNT")
74  	private Integer retryCount;
75  	@Version
76  	@Column(name="VER_NBR")
77  	private Integer lockVerNbr;
78      @Column(name="IP_NBR")
79  	private String ipNumber;
80      @Column(name="SVC_NM")
81  	private String serviceName;
82      @Column(name="APPL_ID")
83  	private String applicationId;
84      @Column(name="SVC_MTHD_NM")
85  	private String methodName;
86      @Transient
87      private AsynchronousCall methodCall;
88      @Transient
89      private PersistedMessagePayload payload;
90      @Column(name="APP_VAL_ONE")
91  	private String value1;
92      @Column(name="APP_VAL_TWO")
93  	private String value2;
94      
95      public PersistedMessageBO() {
96          // default constructor
97      }
98  
99      public static PersistedMessageBO buildMessage(ServiceConfiguration serviceConfiguration, AsynchronousCall methodCall) {
100         PersistedMessageBO message = new PersistedMessageBO();
101         message.setPayload(new PersistedMessagePayload(methodCall, message));
102         message.setIpNumber(RiceUtilities.getIpNumber());
103         message.setServiceName(serviceConfiguration.getServiceName().toString());
104         message.setQueueDate(new Timestamp(System.currentTimeMillis()));
105         if (serviceConfiguration.getPriority() == null) {
106             message.setQueuePriority(KSBConstants.ROUTE_QUEUE_DEFAULT_PRIORITY);
107         } else {
108             message.setQueuePriority(serviceConfiguration.getPriority());
109         }
110         message.setQueueStatus(KSBConstants.ROUTE_QUEUE_QUEUED);
111         message.setRetryCount(0);
112         if (serviceConfiguration.getMillisToLive() > 0) {
113             message.setExpirationDate(new Timestamp(System.currentTimeMillis() + serviceConfiguration.getMillisToLive()));
114         }
115         message.setApplicationId(CoreConfigHelper.getApplicationId());
116         message.setMethodName(methodCall.getMethodName());
117         return message;
118     }
119     
120     //@PrePersist
121     public void beforeInsert(){
122         OrmUtils.populateAutoIncValue(this, KSBServiceLocator.getMessageEntityManagerFactory().createEntityManager());
123     }
124     
125 	@Override
126     public String getApplicationId() {
127 		return this.applicationId;
128 	}
129 
130 	public void setApplicationId(String applicationId) {
131 		this.applicationId = applicationId;
132 	}
133 
134     @Override
135     public String getIpNumber() {
136         return this.ipNumber;
137     }
138     
139     public void setIpNumber(String ipNumber) {
140         this.ipNumber = ipNumber;
141     }
142     
143 	@Override
144     public Timestamp getQueueDate() {
145 		return this.queueDate;
146 	}
147 
148 	@Override
149     public Integer getQueuePriority() {
150 		return this.queuePriority;
151 	}
152 
153 	@Override
154     public String getQueueStatus() {
155 		return this.queueStatus;
156 	}
157 
158 	@Override
159     public Integer getRetryCount() {
160 		return this.retryCount;
161 	}
162 
163 
164 	public void setQueueDate(Timestamp timestamp) {
165 	    this.queueDate = timestamp;
166 	}
167 
168 	public void setQueuePriority(Integer integer) {
169 	    this.queuePriority = integer;
170 	}
171 
172 	public void setQueueStatus(String string) {
173 	    this.queueStatus = string;
174 	}
175 
176 	public void setRetryCount(Integer integer) {
177 	    this.retryCount = integer;
178 	}
179 
180 
181     public Integer getLockVerNbr() {
182         return this.lockVerNbr;
183     }
184     
185     public void setLockVerNbr(Integer lockVerNbr) {
186         this.lockVerNbr = lockVerNbr;
187     }
188     
189     @Override
190     public Long getRouteQueueId() {
191         return this.routeQueueId;
192     }
193     
194     public void setRouteQueueId(Long queueSequence) {
195         this.routeQueueId = queueSequence;
196     }
197     
198 	@Override
199     public String getServiceName() {
200 		return this.serviceName;
201 	}
202 
203 	public void setServiceName(String serviceName) {
204 		this.serviceName = serviceName;
205 	}
206 
207     public String toString() {
208 	return "[RouteQueue: " + ", routeQueueId=" + this.routeQueueId + ", ipNumber=" + this.ipNumber 
209 		+ "applicationId=" + this.applicationId + ", serviceName=" + this.serviceName + ", methodName=" + methodName 
210 		+ ", queueStatus=" + this.queueStatus + ", queuePriority=" + this.queuePriority
211 		+ ", queueDate=" + this.queueDate + "]";
212     }
213 
214 	@Override
215     public AsynchronousCall getMethodCall() {
216 		return this.methodCall;
217 	}
218 
219 	public void setMethodCall(AsynchronousCall methodCall) {
220 		this.methodCall = methodCall;
221 	}
222 
223 	@Override
224     public String getMethodName() {
225 		return this.methodName;
226 	}
227 
228 	public void setMethodName(String methodName) {
229 		this.methodName = methodName;
230 	}
231 
232 	@Override
233     public Timestamp getExpirationDate() {
234 		return this.expirationDate;
235 	}
236 
237 	public void setExpirationDate(Timestamp expirationDate) {
238 		this.expirationDate = expirationDate;
239 	}
240 
241     @Override
242     public PersistedMessagePayload getPayload() {
243 	if (this.payload == null) {
244 	    if (this.getRouteQueueId() == null) {
245 		return null;
246 }	    this.payload = KSBServiceLocator.getMessageQueueService().findByPersistedMessageByRouteQueueId(this.getRouteQueueId()); 
247 	}
248         return this.payload;
249     }
250 
251     public void setPayload(PersistedMessagePayload payload) {
252         this.payload = payload;
253     }
254 
255     @Override
256     public String getValue1() {
257         return this.value1;
258     }
259 
260     public void setValue1(String value1) {
261         this.value1 = value1;
262     }
263 
264     @Override
265     public String getValue2() {
266         return this.value2;
267     }
268 
269     public void setValue2(String value2) {
270         this.value2 = value2;
271     }
272 
273 }