ActiveMq实现图片传输的过程。

来源:互联网 发布:淘宝主店铺旺旺怎么找 编辑:程序博客网 时间:2024/04/30 22:02

最近在学习activeMq,关于很多的文章卧我相信很多想去了解这个消息队列的人都会去看,先是看的zeroMq,目前用的就是zeroMq.但是为了不局限雨zeroMq,于是自己学习了这个。

这里简单的做了消息的传输和图片的传输:

相信关于activeMQ的安装已经很多啦!我这里用的是apache-activemq-5.9.0-bin这个版本的,下载安装。

这里我就不详细说了。

我这里主要处理的是信息接收和图片转化发送:

发送者:

package com.foxx;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;


import javax.imageio.ImageIO;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;


import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
 
/**
 *  消息发送者
 */
public class SenderHead {
 
    // 发送次数
    public static final int SEND_NUM = 5;
    // tcp 地址
    public static final String BROKER_URL = "tcp://localhost:61616";
    // 目标,在ActiveMQ管理员控制台创建 http://localhost:8161/admin/queues.jsp
    public static final String DESTINATION = "hoo.mq.queue";
    static BASE64Encoder encoder = new sun.misc.BASE64Encoder();  
    static BASE64Decoder decoder = new sun.misc.BASE64Decoder();  
    
    public static void sendMessage(Session session, MessageProducer producer) throws Exception {
     File f = new File("D:/timg.jpg");  
          BufferedImage bi;  
          try {  
              bi = ImageIO.read(f);  
              ByteArrayOutputStream baos = new ByteArrayOutputStream();  
              ImageIO.write(bi, "jpg", baos);  //经测试转换的图片是格式这里就什么格式,否则会失真  
              byte[] bytes = baos.toByteArray();  
    
              String mes= encoder.encodeBuffer(bytes).trim()+"图片";
//        for (int i = 0; i < 100; i++) {
//            String message = "发送消息第" + (i + 1) + "条";
            TextMessage text = session.createTextMessage(mes);
            System.out.println(mes);
            producer.send(text);
//        }
          }catch (Exception e) {
System.out.println(e.toString());
}
    }
    
    public static void run() throws Exception {
        
        Connection connection = null;
        Session session = null;
        try {
          
            ConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, BROKER_URL);

            connection = factory.createConnection();

            connection.start();
            // 创建一个session会话
            session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            // 创建一个消息队列
            Destination destination = session.createQueue(DESTINATION);
            // 创建消息制作者
            MessageProducer producer = session.createProducer(destination);
            // 设置持久化模式
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            sendMessage(session, producer);
            // 提交会话
            session.commit();
            
        } catch (Exception e) {
            throw e;
        } finally {
            // 关闭释放资源
            if (session != null) {
                session.close();
            }
            if (connection != null) {
                connection.close();
            }
        }
    }
    
    public static void main(String[] args) throws Exception {
        SenderHead.run();
    }
}


接受者:


package com.foxx;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;


import javax.imageio.ImageIO;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;


import sun.misc.BASE64Decoder;
 
public class MessageReceiver {
 
    // tcp 地址
    public static final String BROKER_URL = "tcp://localhost:61616";
    // 目标,在ActiveMQ管理员控制台创建 http://localhost:8161/admin/queues.jsp
    public static final String DESTINATION = "hoo.mq.queue";
    static BASE64Decoder decoder = new sun.misc.BASE64Decoder(); 
    
    public static void run() throws Exception {
        
        Connection connection = null;
        Session session = null;
        try {
            // 创建链接工厂
            ConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, BROKER_URL);
            // 通过工厂创建一个连接
            connection = factory.createConnection();
            // 启动连接
            connection.start();
            // 创建一个session会话
            session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            // 创建一个消息队列
            Destination destination = session.createQueue(DESTINATION);
            // 创建消息制作者
            MessageConsumer consumer = session.createConsumer(destination);
            
            while (true) {
            Message message = consumer.receive(1000 * 100);
            TextMessage text = (TextMessage) message;
                 if (text != null) {
                     System.out.println("接收:" + text.getText());
                 } else {
                     break;
                 }
                // 接收数据的时间(等待) 100 ms
               
                try {  
                    byte[] bytes1 = decoder.decodeBuffer(text.getText());  
          
                    ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);  
                    BufferedImage bi1 = ImageIO.read(bais);  
                    File w2 = new File("H://QQ1.jpg");// 可以是jpg,png,gif格式  
                    ImageIO.write(bi1, "jpg", w2);// 不管输出什么格式图片,此处不需改动  
                    System.out.println("图片保存成功");
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
               
            }
            
            // 提交会话
            session.commit();
            
        } catch (Exception e) {
            throw e;
        } finally {
            // 关闭释放资源
            if (session != null) {
                session.close();
            }
            if (connection != null) {
                connection.close();
            }
        }
    }
    
    public static void main(String[] args) throws Exception {
        MessageReceiver.run();
    }
}