MQ发送消息到队列的Java代码详解编程语言

MQ发送消息到队列

 

MsgSend.java        

package com.mq.dpca.msg; 
 
import javax.jms.JMSException; 
import javax.jms.Queue; 
import javax.jms.QueueConnection; 
import javax.jms.QueueSender; 
import javax.jms.QueueSession; 
import javax.jms.Session; 
import javax.jms.TextMessage; 
 
import com.ibm.mq.jms.MQQueueConnectionFactory; 
import com.ibm.msg.client.wmq.WMQConstants; 
 
/** 
 * MQ字符串发送功能 
 *  
 */ 
public class MsgSend { 
 
	MQQueueConnectionFactory mcf; 
	QueueConnection qconn; 
	MQConfig mc = null; 
 
	/** 
	 * 初始化连接 
	 *  
	 * @throws JMSException 
	 */ 
	public void openConnection() throws JMSException { 
		mc = MQConfig.getInstance(); 
		mcf = new MQQueueConnectionFactory(); 
		mcf.setConnectionNameList(mc.getMQ_HOST_NAME()); 
		// mcf.setHostName(mc.getMQ_HOST_NAME()); 
		mcf.setChannel(mc.getMQ_CHANNEL()); 
		mcf.setTransportType(WMQConstants.WMQ_CM_CLIENT); 
		mcf.setClientReconnectOptions(WMQConstants.WMQ_CLIENT_RECONNECT); 
		// mcf.setPort(Integer.parseInt(mc.getMQ_PROT())); 
		mcf.setCCSID(Integer.parseInt(mc.getMQ_CCSID())); 
		mcf.setQueueManager(mc.getMQ_MANAGER()); 
		qconn = mcf.createQueueConnection(); 
	} 
 
	/** 
	 * 发送消息 
	 *  
	 * @param key 
	 * @param msgInfo 
	 * @throws JMSException 
	 * @throws InterruptedException 
	 */ 
	public void send(String key, String msgInfo) { 
		QueueSession session = null; 
		QueueSender sender = null; 
		try { 
			openConnection(); 
			session = qconn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); 
			Queue queue = session.createQueue(mc.getMQ_QUEUE_NAME()); 
			sender = session.createSender(queue); 
			TextMessage msg = session.createTextMessage(); 
			msg.setText(msgInfo); 
			// msg.setStringProperty("Key", key); 
			sender.send(msg); 
 
		} catch (JMSException e) { 
			e.printStackTrace(); 
		} finally { 
			try { 
				sender.close(); 
				session.close(); 
				qconn.close(); 
			} catch (JMSException e) { 
				e.printStackTrace(); 
			} 
		} 
	} 
	/** 
	 * 程序的入口 
	 * @param args 
	 * @throws JMSException 
	 * @throws InterruptedException 
	 */ 
 
	public static void main(String[] args) throws JMSException, 
			InterruptedException { 
		MsgSend ms = new MsgSend(); 
		String str = "<root><esbHeader><requestId>d5d001f47d46e1471ae42ebf1dded7a2</requestId><serviceName>syncCustmoerInfo</serviceName>" 
				+ "<key>3DOxe6EfkvcAAAFH95SjNwAAAAAAAAAD</key></esbHeader><serviceNameReq><security><key>f7b17a6ffcd5561748c5bb98075aa432</key><date>2013-04-10 15:25:00</date></security>" 
				+ "<customer><khmc>测试客户ES2B</khmc><fzxsid>120483321</fzxsid><khczqh>010</khczqh><khczfj>1234</khczfj><khcz>567892012</khcz>" 
				+ "</customer>" 
				+ "<customer><khmc>测试客户ES2B</khmc><fzxsid>120483321</fzxsid><khczqh>010</khczqh><khczfj>1234</khczfj><khcz>567892012</khcz>" 
				+ "</customer>" 
				+ "<customer><khmc>测试客户ES2B</khmc><fzxsid>120483321</fzxsid><khczqh>010</khczqh><khczfj>1234</khczfj><khcz>567892012</khcz>" 
				+ "</customer>" + "</serviceNameReq>" + "</root>"; 
		ms.send("", str); 
		// Thread.sleep(10000); 
		System.out.print("消息发送完毕!"); 
	} 
} 

MQConfig.java ~ 6KB         

package com.mq.dpca.msg; 
 
import java.io.BufferedInputStream; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.Properties; 
import java.util.PropertyResourceBundle; 
import java.util.ResourceBundle; 
 
import com.mq.dpca.util.RenameUtil; 
 
/** 
 * MQ访问配置文件各参数的获取 
 *  
 */ 
public class MQConfig { 
	// MQ配置及server配置文件路径 
	private static final String ACTIONPATH = "config.properties"; 
	private static MQConfig instance = null; 
 
	private String MQ_MANAGER = null; 
	private String MQ_HOST_NAME = null; 
	private String MQ_CHANNEL = null; 
	private String MQ_QUEUE_NAME = null; 
	private String MQ_PROT = null; 
	private String MQ_CCSID = null; 
	private String MQ_QUEUE_SUB = null; 
	private String FILE_DIR = null; 
 
	public String getFILE_DIR() { 
		return FILE_DIR; 
	} 
 
	public void setFILE_DIR(String fILE_DIR) { 
		FILE_DIR = fILE_DIR; 
	} 
 
	public String getMQ_MANAGER() { 
		return MQ_MANAGER; 
	} 
 
	public void setMQ_MANAGER(String mq_manager) { 
		MQ_MANAGER = mq_manager; 
	} 
 
	public String getMQ_HOST_NAME() { 
		return MQ_HOST_NAME; 
	} 
 
	public void setMQ_HOST_NAME(String mq_host_name) { 
		MQ_HOST_NAME = mq_host_name; 
	} 
 
	public String getMQ_CHANNEL() { 
		return MQ_CHANNEL; 
	} 
 
	public void setMQ_CHANNEL(String mq_channel) { 
		MQ_CHANNEL = mq_channel; 
	} 
 
	public String getMQ_QUEUE_NAME() { 
		return MQ_QUEUE_NAME; 
	} 
 
	public void setMQ_QUEUE_NAME(String mq_queue_name) { 
		MQ_QUEUE_NAME = mq_queue_name; 
	} 
 
	public String getMQ_PROT() { 
		return MQ_PROT; 
	} 
 
	public void setMQ_PROT(String mq_prot) { 
		MQ_PROT = mq_prot; 
	} 
 
	public String getMQ_CCSID() { 
		return MQ_CCSID; 
	} 
 
	public void setMQ_CCSID(String mq_ccsid) { 
		MQ_CCSID = mq_ccsid; 
	} 
 
	public static MQConfig getInstance() { 
		if (instance == null) { 
			instance = new MQConfig().getNewDbConfig(); 
		} 
		return instance; 
	} 
	 
	public static ResourceBundle getCFG(){ 
		String cfgPath = ""; 
		InputStream in = null; 
		ResourceBundle pathCfg = null; 
		ResourceBundle mqCfg = null; 
		pathCfg = PropertyResourceBundle.getBundle("config"); 
		cfgPath = RenameUtil.getParameter(pathCfg, "mqcfgPath"); 
		try { 
			in = new BufferedInputStream(new FileInputStream(cfgPath)); 
			mqCfg = new PropertyResourceBundle(in); 
			return mqCfg; 
		} catch (FileNotFoundException e) { 
			e.printStackTrace(); 
		}catch (IOException e) { 
			e.printStackTrace(); 
		} 
		return mqCfg; 
	} 
 
	public static MQConfig getNewDbConfig() { 
 
		MQConfig dc = new MQConfig(); 
		Properties prop = new Properties(); 
		String path = null; 
		InputStream fis = null; 
 
		try { 
			fis = MQConfig.class.getClassLoader().getResourceAsStream( 
					"config.properties"); 
			// fis = new FileInputStream(new File(path + ACTIONPATH)); 
			prop.load(fis); 
			dc.MQ_CCSID = prop.getProperty("MQ_CCSID"); 
			dc.MQ_CHANNEL = prop.getProperty("MQ_CHANNEL"); 
			dc.MQ_HOST_NAME = prop.getProperty("MQ_HOST_NAME"); 
			dc.MQ_MANAGER = prop.getProperty("MQ_MANAGER"); 
			dc.MQ_PROT = prop.getProperty("MQ_PROT"); 
			dc.MQ_QUEUE_NAME = prop.getProperty("MQ_QUEUE_NAME"); 
			dc.MQ_QUEUE_SUB = prop.getProperty("MQ_QUEUE_SUB"); 
			dc.FILE_DIR = prop.getProperty("FILE_DIR"); 
		} catch (FileNotFoundException e) { 
			e.printStackTrace(); 
		} catch (IOException e) { 
			e.printStackTrace(); 
		} 
 
		return dc; 
	} 
	 
	public static MQConfig MqConfig() { 
 
		MQConfig dc = new MQConfig(); 
		ResourceBundle rb =  getCFG(); 
		try { 
			// fis = new FileInputStream(new File(path + ACTIONPATH)); 
			dc.MQ_CCSID = RenameUtil.getParameter(rb,"MQ_CCSID"); 
			dc.MQ_CHANNEL = RenameUtil.getParameter(rb,"MQ_CHANNEL"); 
			dc.MQ_HOST_NAME = RenameUtil.getParameter(rb,"MQ_HOST_NAME"); 
			dc.MQ_MANAGER = RenameUtil.getParameter(rb,"MQ_MANAGER"); 
			dc.MQ_PROT = RenameUtil.getParameter(rb,"MQ_PROT"); 
			dc.MQ_QUEUE_NAME = RenameUtil.getParameter(rb,"MQ_QUEUE_NAME"); 
			dc.MQ_QUEUE_SUB = RenameUtil.getParameter(rb,"MQ_QUEUE_SUB"); 
			dc.FILE_DIR = RenameUtil.getParameter(rb,"FILE_DIR"); 
		} catch (Exception e) { 
			e.printStackTrace(); 
		} 
		return dc; 
	} 
 
	public static MQConfig getNewDbConfigFromKey(String key) { 
		MQConfig dc = null; 
		Properties prop = new Properties(); 
		String path = null; 
		InputStream fis = null; 
		try { 
			fis = MQConfig.class.getClassLoader().getResourceAsStream( 
					"config.properties"); 
			// fis = new FileInputStream(new File(path + ACTIONPATH)); 
			prop.load(fis); 
			dc = new MQConfig(); 
			dc.MQ_MANAGER = prop.getProperty(key + "_MQ_MANAGER"); 
			dc.MQ_CCSID = prop.getProperty(key + "_MQ_CCSID"); 
			dc.MQ_CHANNEL = prop.getProperty(key + "_MQ_CHANNEL"); 
			dc.MQ_HOST_NAME = prop.getProperty(key + "_MQ_HOST_NAME"); 
			dc.MQ_PROT = prop.getProperty(key + "_MQ_PROT"); 
			dc.MQ_QUEUE_NAME = prop.getProperty(key + "_MQ_QUEUE_NAME"); 
			dc.MQ_QUEUE_SUB = prop.getProperty(key + "_MQ_QUEUE_SUB"); 
			dc.FILE_DIR = prop.getProperty(key + "_FILE_DIR"); 
		} catch (FileNotFoundException e) { 
			dc = null; 
		} catch (IOException e) { 
			dc = null; 
		} 
 
		return dc; 
	} 
 
	public static void main(String args[]) throws Exception { 
		MQConfig mc = MQConfig.getNewDbConfigFromKey("002"); 
		System.out.println(mc); 
	} 
 
	public void setMQ_QUEUE_SUB(String mQ_QUEUE_SUB) { 
		MQ_QUEUE_SUB = mQ_QUEUE_SUB; 
	} 
 
	public String getMQ_QUEUE_SUB() { 
		return MQ_QUEUE_SUB; 
	} 
 
	public static String getValueByKey(String key) { 
		InputStream fis = null; 
		String value = null; 
		Properties prop = new Properties(); 
		try { 
			fis = MQConfig.class.getClassLoader().getResourceAsStream( 
					"config.properties"); 
			prop.load(fis); 
			value = prop.getProperty(key); 
		} catch (FileNotFoundException e) { 
			e.printStackTrace(); 
		} catch (IOException e) { 
			e.printStackTrace(); 
		} 
		return value; 
	} 
} 

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/10202.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论