「jmsjava实例」jms开发
本篇文章给大家谈谈jmsjava实例,以及jms开发对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
java什么叫类属性 实例属性 以及区别
一个是类的方法(也叫动态属性)和属性(静态属性),通过类名来访问
一个是对象的方法和属性,需要通过一个实例来访问。
静态属性程序一加载时 就初始化 存放在栈中
实例属性 需要实例化后 才加载 存放在堆中
谁有IBM MQ 和 java jms 的例子啊
别人弄的,我现在还在调,还没有通过。下面的代码应该没有问题
/**
* A WebSphere MQ application which uses MQQueueConnectionFactory to send msg to a Queue.
* This MQQueueConnectionFactory is looked up in JNDI Context.
* Before you look up, you should create a JNDI Context using 'WebSphere MQ JMS administration tool'.
* This method to create is provided at other file.
*/
package com.ibm.mq.test;
import java.util.Hashtable;
import javax.jms.DeliveryMode;
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 javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.ibm.mq.jms.MQQueueConnectionFactory;
public class MQSendByCF_JNDI {
public static void main(String[] args) {
MQSendByCF_JNDI sender = new MQSendByCF_JNDI();
try {
sender.initMQObjects();
sender.sendMsg();
} catch(Exception e) {
e.printStackTrace();
} finally {
sender.closeMQObjects();
}
}
MQQueueConnectionFactory mqcf;
QueueConnection conn;
QueueSession session;
TextMessage textMsg;
Queue queue;
QueueSender sender;
public void initMQObjects() throws Exception {
String strMsg = "Where are you?";
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory");
env.put(Context.PROVIDER_URL, "file:/D:/JNDI");
try {
Context ctx = new InitialContext(env);
mqcf = (MQQueueConnectionFactory) ctx.lookup("QCF_TEST");
/*This Queue [Q_TEST] is not a real queue on QebSphere MQ,
but it is binded to a real queue on WebSphere MQ.*/
queue = (Queue) ctx.lookup("Q_TEST");
} catch (NamingException e) {
System.out.println("Find MQ Objects from Context Failed.");
throw e;
}
conn = mqcf.createQueueConnection();
session = conn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
textMsg = session.createTextMessage(strMsg);
sender = session.createSender(queue);
}
/**
* Name: sendMsg
* Date: 2010-10-1
* paramter:
* Return_type: void
* User: Leng Fuping
* Description: Send Msg
*/
public void sendMsg() throws Exception {
try {
conn.start();
} catch (JMSException e) {
System.out.println("Send Msg Failed.");
throw e;
}
sender.send(textMsg,DeliveryMode.NON_PERSISTENT,0,0);
System.out.println("Send Msg succeed.");
}
/**
* Name: closeMQObjects
* Date: 2010-10-1
* paramter:
* Return_type: void
* User: Leng Fuping
* Description: Close MQ Objects
*/
public void closeMQObjects() {
try {
if(conn != null) {
conn.stop();
}
if(sender != null) {
sender.close();
}
if(session != null) {
session.close();
}
if(conn != null) {
conn.close();
}
if(mqcf != null) {
mqcf.clear();
}
System.out.println("Close MQ objects succeed.");
} catch (JMSException e) {
System.out.println("Close MQ objects failed: " + e.getMessage());
}
}
}
什么是JMS消息服务(Java Message Service)?
打个比方。你家里人都在家。那你们聊个天什么的,直接说就可以了。
但是如果你父母在家,你在外地,那要聊个天什么的,吼两句就没办法了。这时候就是要借助电话或者互联网这类工具了。这类工具会把你的消息传达给你的父母,再把你父母的消息传递给你。
而JMS就是这么一个功能。负责多方的消息传输。
一般做分布式,都是把多层应用放到多个服务器上去。比如说,前台服务器接受到用户请求。会先去缓存服务器查一下该页面有没有缓存。那这个时候就要跟缓存服务器通信。发出查询是否有缓存的请求。那你就要建一个这样的服务,让前台可查询。查询后,缓存服务器又要响应结果。
如果没有缓存的话,那这个时候又要与业务层打交道,业务层再与数据层请求。这层与层之间,或者服务器与服务器间通讯,都需要有一套的通讯框架。JSM就是做这个了。
jmsjava实例的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于jms开发、jmsjava实例的信息别忘了在本站进行查找喔。
发布于:2022-12-01,除非注明,否则均为
原创文章,转载请注明出处。