activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From SuoNayi <suonayi2...@163.com>
Subject Re:about blobmessage
Date Thu, 23 May 2013 04:18:21 GMT
try out the JMS Streams,http://activemq.apache.org/jms-streams.html



At 2013-05-23 11:51:23,luodaidong <luodaidong@sina.com> wrote:
>I use activeMQ blobmessage tranfering large file. It's OK, but the receiver
>can receive file only when the sender has sended the file completly. How
>about realtime transfer large file? where can I configure this?
>the test code following,where is wrong?
>
>the sender
>
>package mytest.file;  
>  
>import java.io.File;
>
>import javax.jms.Connection;
>import javax.jms.ConnectionFactory;
>import javax.jms.DeliveryMode;
>import javax.jms.Destination;
>import javax.jms.JMSException;
>import javax.jms.MessageProducer;
>import javax.jms.Session;
>import javax.swing.JFileChooser;
>
>import org.apache.activemq.ActiveMQConnectionFactory;
>import org.apache.activemq.ActiveMQSession;
>import org.apache.activemq.BlobMessage;
>  
>/** 
> * 通过 ActiveMQ 发送文件的程序 
> *  
> * @author hailiang 
> */  
>public class FileSender {  
>  
>    /** 
>     * @param args 
>     * @throws JMSException 
>     */  
>    public static void main(String[] args) throws JMSException {  
>		System.out.println("1111");
>        // 选择文件   
>        JFileChooser fileChooser = new JFileChooser();  
>        fileChooser.setDialogTitle("选择文件");  
>        if (fileChooser.showOpenDialog(null) != JFileChooser.APPROVE_OPTION)
>{  
>            return;  
>        }  
>        File file = fileChooser.getSelectedFile();  
>  
>        // 获取 ConnectionFactory   
>        ConnectionFactory connectionFactory = new
>ActiveMQConnectionFactory("admin","admin" , 
>               
>"tcp://192.168.17.4:61616?jms.blobTransferPolicy.defaultUploadUrl=http://admin:admin@192.168.17.4:8161/fileserver/");
 
>  
>        // 创建 Connection   
>        Connection connection = connectionFactory.createConnection();  
>        connection.start();  
>  
>        // 创建 Session   
>        ActiveMQSession session = (ActiveMQSession)
>connection.createSession(  
>                false, Session.AUTO_ACKNOWLEDGE);  
>  
>        // 创建 Destination   
>        Destination destination = session.createQueue("File.Transport");  
>  
>        // 创建 Producer   
>        MessageProducer producer = session.createProducer(destination);  
>        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);// 设置为非持久性
  
>        // 设置持久性的话,文件也可以先缓存下来,接收端离线再连接也可以收到文件
  
>  
>        // 构造 BlobMessage,用来传输文件   
>        BlobMessage blobMessage = session.createBlobMessage(file);  
>        blobMessage.setStringProperty("FILE.NAME", file.getName());  
>        blobMessage.setLongProperty("FILE.SIZE", file.length());  
>        System.out.println("开始发送文件:" + file.getName() + ",文件大小:"
 
>                + file.length() + " 字节");  
>  
>        // 7. 发送文件   
>        producer.send(blobMessage);  
>        System.out.println("完成文件发送:" + file.getName());  
>  
>        producer.close();  
>        session.close();  
>        connection.close(); // 不关闭 Connection, 程序则不退出   
>    }  
>}  
>
>
>the receiver
>package mytest.file;    
>import java.io.*;  
>import javax.jms.*;  
>import javax.jms.Message;  
>import javax.swing.*;  
>import org.apache.activemq.*;    
>
>  
>public class FileReciever {  
>  
>    /** 
>     * @param args 
>     * @throws JMSException 
>     */  
>    public static void main(String[] args) throws JMSException {  
>  
>        // 获取 ConnectionFactory   
>        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(  
>                "tcp://192.168.17.4:61616");  
>  
>        // 创建 Connection   
>        Connection connection = connectionFactory.createConnection();  
>        connection.start();  
>  
>        // 创建 Session   
>        Session session = connection.createSession(false,  
>                Session.AUTO_ACKNOWLEDGE);  
>  
>        // 创建 Destinatione   
>        Destination destination = session.createQueue("File.Transport");  
>  
>        // 创建 Consumer   
>        MessageConsumer consumer = session.createConsumer(destination);  
>  
>        // 注册消息监听器,当消息到达时被触发并处理消息   
>        consumer.setMessageListener(new MessageListener() {  
>  
>            // 监听器中处理消息   
>            public void onMessage(Message message) {  
>                if (message instanceof BlobMessage) {  
>                    BlobMessage blobMessage = (BlobMessage) message;  
>                    try {  
>                        String fileName = blobMessage  
>                                .getStringProperty("FILE.NAME");  
>                        System.out.println("文件接收请求处理:" + fileName +
",文件大小:"  
>                                + blobMessage.getLongProperty("FILE.SIZE")  
>                                + " 字节");  
>  
>                        JFileChooser fileChooser = new JFileChooser();  
>                        fileChooser.setDialogTitle("请指定文件保存位置");  
>                        fileChooser.setSelectedFile(new File(fileName));  
>                        if (fileChooser.showSaveDialog(null) ==
>JFileChooser.APPROVE_OPTION) {  
>                            File file = fileChooser.getSelectedFile();  
>                            OutputStream os = new FileOutputStream(file);  
>  
>                            System.out.println("开始接收文件:" + fileName);  
>                            InputStream inputStream = blobMessage  
>                                    .getInputStream();  
>  
>                            // 写文件,你也可以使用其他方式   
>                            byte[] buff = new byte[256];  
>                            int len = 0;  
>                            while ((len = inputStream.read(buff)) > 0) {  
>                                os.write(buff, 0, len);  
>                            }  
>                            os.close();  
>                            System.out.println("完成文件接收:" + fileName);  
>                        }  
>  
>                    } catch (Exception e) {  
>                        e.printStackTrace();  
>                    }  
>                }  
>            }  
>        });  
>    }  
>}  
>
>
>
>
>--
>View this message in context: http://activemq.2283324.n4.nabble.com/about-blobmessage-tp4667381.html
>Sent from the ActiveMQ - User mailing list archive at Nabble.com.
Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message