Use Active to send files
ActiveMq provides support for file transfer.
1. directly transfer files: use the form of connection. createOutputStream. This method is suitable for small files. You cannot transmit large files.
2. BlobMessage: (Binary Large OBjects) This method can transmit Large files. In essence, when a BlobMessage is sent, the file is first transmitted to the file server, and then received from the file server. In this way, what we transmit in mq is actually a file ID.
This article introduces the second method. MQ itself comes with the jetty startup method.
ActiveMQConnectionFactory fac = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER, ActiveMQConnectionFactory.DEFAULT_PASSWORD, "tcp://localhost:61616?jms.blobTransferPolicy.defaultUploadUrl=http://localhost:8161/fileserver/");
Connection createConnection = fac.createConnection();
createConnection.start();
ActiveMQSession activeMQSession = (ActiveMQSession) createConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
File file = new File("D:\\1.html");
BlobMessage createBlobMessage = activeMQSession.createBlobMessage(file);
Queue createQueue = activeMQSession.createQueue("test.blobmessage");
MessageProducer createProducer = activeMQSession.createProducer(createQueue);
createProducer.send(createBlobMessage);
createConnection.close();
In this way, a BlobMessage message can be sent, and the receiving process of the receiver is similar. I will not repeat it.
However, when I use the server that comes with ActiveMQ as a file server, the following error occurs during file upload: PUT was not successful: 401 Unauthorized. So I decided to build my own jetty-based file server.
Build a jetty Server
1. First add jetty dependencies or introduce the jetty jar package:
<dependency>
<groupId>org.eclipse.jetty.aggregate</groupId>
<artifactId>jetty-all-server</artifactId>
<version>7.6.4.v20120524</version>
</dependency>
2. svn from the http://svn.apache.org/repos/asf/activemq/trunk/activemq-fileserver/ gets the three classes that MQ provides file services. Introduce your own project
3. Create your own Server class and add the following code:
Server server = new Server(8080);
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(defaultServlet, "/*");
server.setHandler( handler );
server.start();
4. Start the Server class. Note: Modify the URL setting port when ConnectionFactory is created. The above server is set to 8080, so the link address should be:
Tcp: // localhost: 61616? Jms. blobTransferPolicy. defaultUploadUrl = http: // localhost: 8080/fileserver/
5. This completes the process of building an MQ file server. Resend the file and accept the file!
Jetty Embedded Server references: http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty
This article MQ content reference http://blog.csdn.net/kimmking/article/details/9421343