/*
* Created on 2006-2-16
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package ehub.ihub.exchangeManager;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Hashtable;
import javax.jms.BytesMessage;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class SendMessage {
private static String MQ_CHANNEL = "EXAMPLE.CHANNEL";
private static String MQ_MANAGER = "QMGR";
private static String MQ_QUEUE = "EXAMPLE.SENDQUEUE";
private static int MQ_PORT = 4001;
private static String JMS_CONNECTIONFACTORY = "jms/JMSExampleConnectionFactory";
private static String QUEUE_NAME="jms/JMSExampleSendQueue";
public static void sendFileToReceiveQueue(File file) {
try {
Context initContext = new InitialContext();
ConnectionFactory qconFactory = (ConnectionFactory) initContext
.lookup(JMS_CONNECTIONFACTORY);
Connection qcon = qconFactory.createConnection();
Session session = qcon.createSession(false,
Session.AUTO_ACKNOWLEDGE);
Queue queue = (Queue) initContext.lookup(QUEUE_NAME);
MessageProducer producer = session.createProducer(queue);
ObjectMessage outMessage=session.createObjectMessage();
/* write the file information into the message */
Hashtable fileInfo = new Hashtable();
fileInfo.put("FileName", file.getName());
fileInfo.put("FileSize", Long.toString(file.length()));
/* write the file content into the message */
FileInputStream fos = new FileInputStream(file);
byte[] buf;
int size = (int) file.length();
buf = new byte[size];
int num = fos.read(buf);
fos.close();
/*add the file byte stream to the object*/
fileInfo.put("content", buf);
outMessage.setObject(fileInfo);
outMessage.getObject();
outMessage.setJMSCorrelationIDAsBytes((new String("clinet_B_receive")).getBytes());
// qcon.start();
producer.send(outMessage);
producer.close();
session.close();
qcon.close();
} catch (NamingException e) {
System.out.println("
获得连接失败
,jndi
查找失败
");
e.printStackTrace();
} catch (JMSException e) {
System.out.println("
发送文件异常
");
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("
发送文件过程中
io
操作失败
");
e.printStackTrace();
}
}
}
|