Use ActiveMQ to transfer files and use Jetty to build an embedded file server

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.