Blockingqueue can be a limited capacity. It can have a remainingcapacity at any given time, beyond which additional elements cannot be put without interruption. Blockingqueue without any internal capacity constraints always reports the remaining capacity of integer. max_value.
Blockingqueue is mainly used for producer-user queues, but it also supports the collection interface. Therefore, for example, it is possible to remove any element from the queue using remove (X. However, such operations are generally not executed effectively and can only be used occasionally in a planned manner, for example, when queuing information is canceled.
Blockingqueue implementation is thread-safe. All queuing methods can use internal locks or other forms of concurrency control to automatically achieve their purpose. However, a large number of collection operations (addall, containsall, retainall, and removeall) do not require automatic execution unless otherwise specified in the implementation. Therefore, for example, after only some elements in C are added, addall (c) may fail (throwing an exception ).
Blockingqueue does not support any "close" or "shutdown" operation to indicate that no items are added. The demand and use of such features are dependent on implementation. For example, a common policy is to insert a special end-of-stream or poison object to the producer and explain the Objects Based on the time the user obtains them.
The following describes how to use blockingqueue in the project to implement the short message service.
Send SMS queue class [sendmsgqueue. Java]
/*** Sendmsgqueue. java * copyright (c) 2012 * created: cuiran 09:12:17 */package COM. WPN. web. server. messageprocessor; import Java. util. concurrent. arrayblockingqueue; import Java. util. concurrent. blockingqueue; import Org. apache. commons. logging. log; import Org. apache. commons. logging. logfactory; import COM. WPN. cache. common. usercalltaxiinfo; import COM. WPN. web. domain. mytmq. userorderinfo;/*** send SMS queue * @ author cuiran * @ version todo */public class sendmsgqueue {Private Static log = logfactory. getlog (sendmsgqueue. class. getname (); Private Static int queuesize = 1000; Private Static blockingqueue <userorderinfo> queue = new queue <userorderinfo> (queuesize); Public void setqueuesize (INT queuesize) {sendmsgqueue. queuesize = queuesize;}/*** retrieve the user's order information ** @ return */public static userorderinfo getmessage () {userorderinfo Vo = NULL; try {Vo = queue. take ();} catch (interruptedexception e) {log. error ("retrieve user order information", e) ;}return VO ;} /*** Add User order information ** @ Param VO */public static void putmessage (userorderinfo VO) {try {queue. put (VO);} catch (interruptedexception e) {log. error ("Add User order information", e );}}}
Handle processing class for sending SMS reminders to purchased information
/*** Sendmsghandler. java * copyright (c) 2012 * created: cuiran 09:23:28 */package COM. WPN. web. server. messageprocessor; import Org. apache. commons. logging. log; import Org. apache. commons. logging. logfactory; import COM. WPN. cache. common. usercalltaxiinfo; import COM. WPN. web. dao. igiftbaseinfodao; import COM. WPN. web. dao. iuserbaseinfodao; import COM. WPN. web. domain. mytmq. paramcard; import COM. WPN. web. domain. mytmq. userbaseinfo; import COM. WPN. web. domain. mytmq. userorderinfo; import COM. WPN. web. exception. daoexception; import COM. WPN. web. server. union. ismsserver;/*** send SMS reminders for user purchases and gift cards * @ author cuiran * @ version todo */public class sendmsghandler implements runnable {private log = logfactory. getlog (sendmsghandler. class. getname (); Private int ID; private ismsserver SMS; private iuserbaseinfodao userbaseinfodao; private igiftbaseinfodao giftbaseinfodao; @ overridepublic void run () {thread t = thread. currentthread (); T. setname ("Handler-thread:" + id); userorderinfo message = NULL; while (true) {try {message = sendmsgqueue. getmessage (); If (Message = NULL) {continue;} This. processoruserorder (Message);} catch (exception e) {log. error ("", e) ;}} public sendmsghandler (int id, ismsserver SMS, iuserbaseinfodao userbaseinfodao, igiftbaseinfodao giftbaseinfodao) {super (); this. id = ID; this. SMS = SMS; this. userbaseinfodao = userbaseinfodao=this.gif tbaseinfodao = giftbaseinfodao ;}}
Finally, the blockingqueue class is called Based on the short message service class.
/*** Sendmsgservice. java * copyright (c) 2012 * created: cuiran 09:28:34 */package COM. WPN. web. server. messageprocessor; import Java. util. concurrent. executorservice; import Java. util. concurrent. executors; import Org. apache. commons. logging. log; import Org. apache. commons. logging. logfactory; import COM. WPN. web. dao. igiftbaseinfodao; import COM. WPN. web. dao. iuserbaseinfodao; import COM. WPN. web. server. union. ismsserver; import COM. WPN. web. util. handlerthreadfactory;/*** message sending service class * @ author cuiran * @ version todo */public class sendmsgservice {private log = logfactory. getlog (sendmsgservice. class. getname (); Private int threadnumber = 2; Final executorservice exec = executors. newfixedthreadpool (threadnumber, new handlerthreadfactory (); Private ismsserver SMS; private iuserbaseinfodao userbaseinfodao; private igiftbaseinfodao giftbaseinfodao; Public void start () {for (INT I = 0; I <threadnumber; I ++) {exec. submit (New sendmsghandler (I, SMS, userbaseinfodao, giftbaseinfodao) ;}} public void stop () {exec. shutdown ();}/*** @ return the SMS */Public ismsserver getsms () {return SMS ;} /*** @ Param SMS the SMS to set */Public void setsms (ismsserver SMS) {This. SMS = SMS;}/*** @ return the userbaseinfodao */Public iuserbaseinfodao getuserbaseinfodao () {return userbaseinfodao ;} /*** @ Param userbaseinfodao the userbaseinfodao to set */Public void setuserbaseinfodao (iuserbaseinfodao userbaseinfodao) {This. userbaseinfodao = userbaseinfodao;}/*** @ return the giftbaseinfodao */Public igiftbaseinfodao getgiftbaseinfodao () {return giftbaseinfodao ;} /*** @ Param giftbaseinfodao the giftbaseinfodao to set */Public void setgiftbaseinfodao (igiftbaseinfodao giftbaseinfodao) implements this.gif tbaseinfodao = giftbaseinfodao ;}}
It should be noted that both the program interface and Dao interface adopt the Spring IoC method.