JMS两种消息类型的相关代码(可以直接复制粘贴运行)准备介质:myeclipse10+weblogic12c
项目截图:
jndi.properties的内容:java.naming.factory.initial=weblogic.jndi.WLInitialContextFactory
java.naming.provider.url=t3\://127.0.0.1\:7001
相关代码如下(仅供测试,代码没有问题):
①、Queue类型:
SendMessage.java
package com.landingbj.queue;
import java.util.Properties;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
public class SendMessage {
public static void main(String[] args) {
QueueSender sender;// 消息的发送者
TextMessage msg = null;// 消息
try {
Properties properties = new Properties();
Context context = new InitialContext();
// 通过广场获得链接,通过链接获得会话
QueueConnectionFactory queueFactory = (QueueConnectionFactory) context
.lookup("ConnectionFactory-jms");
QueueConnection qConnection = queueFactory.createQueueConnection();
QueueSession qsession = qConnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
// 获得消息队列Queue-jms为自己绑定的JNDI名称
Queue que = (Queue) context.lookup("Queue-jms");
// 建立目的地和消息
sender = qsession.createSender(que);
msg = qsession.createTextMessage();
// 消息内容
msg.setText("aaaddjcijd22jhsiodj");
sender.send(msg);
System.out.println("successful!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
ReceiveMessage.java
package com.landingbj.queue;
import java.util.Properties;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class ReceiveMessage {
public static void main(String[] args) throws JMSException {
QueueReceiver receiver;
TextMessage msg;
try {
Properties properties=new Properties();
Context ctx = new InitialContext();
QueueConnectionFactory qcf = (QueueConnectionFactory) ctx
.lookup("ConnectionFactory-jms");
QueueConnection qc = qcf.createQueueConnection();
QueueSession qsession = qc.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
qc.start();
Queue que = (Queue) ctx.lookup("Queue-jms");
receiver = qsession.createReceiver(que);
msg = (TextMessage) receiver.receive();
System.out.println("你发的消息是..." + msg.getText());
} catch (NamingException e) {
e.printStackTrace();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
②、Topic类型
TopicProducer.java
package com.landingbj.topic;
import java.util.Properties;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.jms.TopicConnectionFactory;
public class TopicProducer {
public static void main(String[] args) {
TextMessage msg = null;// 消息
try {
Properties properties=new Properties();
Context context = new InitialContext();
// 通过工厂获得连接,通过连接获得会话
TopicConnectionFactory topicFactory = (TopicConnectionFactory) context.lookup("ConnectionFactory-jms");
TopicConnection topConnection = topicFactory.createTopicConnection();
TopicSession session = topConnection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
topConnection.start();
Topic topic = (Topic) context.lookup("Topic-jms");
TopicPublisher publisher = session.createPublisher(topic);
msg = session.createTextMessage();
msg.setText("我的public model!");// 消息内容
publisher.publish(msg);
System.out.println("successful!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
TopicReceives.java
package com.landingbj.topic;
import java.util.Properties;
import javax.jms.Message;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.naming.Context;
import javax.naming.InitialContext;
public class TopicReceives {
public static void main(String[] args) {
try {
Properties properties=new Properties();
Context context = new InitialContext();
TopicConnectionFactory factory = (TopicConnectionFactory) context.lookup("ConnectionFactory-jms");
TopicConnection conn = factory.createTopicConnection();
TopicSession session = conn.createTopicSession(false,TopicSession.AUTO_ACKNOWLEDGE);
Topic messageTopic = (Topic) context.lookup("Topic-jms");
TopicSubscriber subscriber = session.createSubscriber(messageTopic);
conn.start();
Message msg = subscriber.receive();
String msgText = ((TextMessage)msg).getText();
Thread.sleep(2000);
System.out.println("jun--" + msgText);
} catch (Exception e) {
e.printStackTrace();
}
}
}
TopicReceives2.java
package com.landingbj.topic;
import java.util.Properties;
import javax.jms.Message;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.naming.Context;
import javax.naming.InitialContext;
public class TopicReceives2 {
public static void main(String[] args) {
try {
Properties properties=new Properties();
Context context = new InitialContext();
TopicConnectionFactory factory = (TopicConnectionFactory) context.lookup("ConnectionFactory-jms");
TopicConnection conn = factory.createTopicConnection();
TopicSession session = conn.createTopicSession(false,TopicSession.AUTO_ACKNOWLEDGE);
Topic messageTopic = (Topic) context.lookup("Topic-jms");
TopicSubscriber subscriber = session.createSubscriber(messageTopic);
conn.start();
Message msg = subscriber.receive();
String msgText = ((TextMessage)msg).getText();
System.out.println("TopicReceives2--" + msgText);
Thread.sleep(4000);
System.out.println("aaa");
} catch (Exception e) {
e.printStackTrace();
}
}
}
相关概念请参看:
该贴被lijun.fang编辑于2015-4-3 12:12:59