The principle and merits of activemq file transmission mentioned above mentioned blobmessage, which uses fileserver to transfer files efficiently.
In fact, the fileserver demo is already available in the Web Console provided by activemq, which is located in webapps under the activemq installation directory.
When activemq is started, if Jetty. XML is imported in the configuration file, the fileserver is automatically loaded and can be used.
In addition, if activemq is used in the embedded environment, you can simply use the fileserver in a completely embedded way.
Two steps are required:
1. Get the fileserver'sCode. It is very simple. There are three classes and no dependent packages are required.
Get these three classes from the http://svn.apache.org/repos/asf/activemq/trunk/activemq-fileserver/, You Can MVN Eclipse: Eclipse mode into the project and then reference,
You can also copy these classes directly to your project code.
2. Start jetty and load fileserver in Embedded Mode
Server = new server (8162); servletcontexthandler handler = new servletcontexthandler (); handler. setresourcebase (". "); handler. setcontextpath ("/fileserver"); system. out. println (handler. getservletcontext (). getrealpath ("/"); handler. addfilter (Org. apache. activemq. util. filenameguardfilter. class, "/*", dispatchertype. forward. ordinal (); handler. addfilter (Org. apache. activemq. util. restfilter. class, "/*", dispatchertype. forward. ordinal (); servletholder DefaultServlet = new servletholder (); DefaultServlet. setname ("DefaultServlet"); DefaultServlet. setclassname ("org. eclipse. jetty. servlet. defaultServlet "); handler. addservlet (defaservlet servlet, "/*"); server. sethandler (handler); server. start ();
Then you can start fileserver. You can perform a simple test.
The procedure is as follows:
1. Start activemq manually, and embed or independently.
2. Run the above Code to start jetty.
3. Use the following code to send the receiving file:
Package KK; import Java. io. file; import Java. io. inputstream; import Java. util. list; import javax. JMS. message; import javax. JMS. messageconsumer; import javax. JMS. messagelistener; import javax. JMS. messageproducer; import javax. JMS. queue; import javax. JMS. session; import Org. apache. activemq. activemqconnection; import Org. apache. activemq. activemqconnectionfactory; import Org. apache. activemq. activemqsession; import Org. apache. activemq. blobmessage; import Org. apache. activemq. command. activemqblobmessage; import Org. apache. activemq. command. activemqqueue; import Org. apache. commons. io. ioutils; public class testblob {public static void main (string [] ARGs) {try {activemqconnectionfactory factorya = new activemqconnectionfactory ("TCP: // 127.0.0.1: 61616? JMS. blobtransferpolicy. defaultuploadurl = http: // localhost: 8162/fileserver/"); queue = new activemqqueue (" blob. KK "); activemqconnection conn = (activemqconnection) factorya. createconnection (); Conn. start (); activemqsession session = (activemqsession) Conn. createsession (false, session. auto_acknowledge); messageconsumer consumer = session. createconsumer (Queue); messagelistener listener = new messagelistener () {public void onmessage (message) {try {system. out. println ("=> receive from blob. KK: "); If (Message instanceof blobmessage) {system. out. println ("filename:" + message. getstringproperty ("file. name "); system. out. println ("filesize:" + message. getlongproperty ("file. size "); blobmessage = (blobmessage) message; inputstream in = blobmessage. getinputstream (); List list = ioutils. readlines (in); For (Object S: List) system. out. println (s); In. close (); (activemqblobmessage) blobmessage ). deletefile (); // Delete the Server File manually after processing} catch (exception e) {e. printstacktrace () ;}}; consumer. setmessagelistener (listener); file = new file ("D: // y.txt"); messageproducer producer = session. createproducer (Queue); blobmessage = session. createblobmessage (File); blobmessage. setstringproperty ("file. name ", file. getname (); blobmessage. setlongproperty ("file. size ", file. length (); producer. send (blobmessage);} catch (exception e) {e. printstacktrace ();}}}
Note that the broker does not automatically delete files. You need to manually delete the files:
(Activemqblobmessage) blobmessage). deletefile (); // Delete the server files manually after processing
4. Run testblob to view the result:
=> Receive from blob. kk:
Filename: y.txt
Filesize: 39
Hello, blobmessage and jetty fileserver
Before the file is successfully consumed, you can see a file similar to the following in the fileserver running project folder: ID_kimmking-33950-1374560343447-1_1_1_1_1
The relationship between the file name and the message is filename = MSG. getjmsmessageid (). tostring (). Replace (":","_")