- 浏览: 40454 次
- 性别:
- 来自: 武汉
文章分类
- 全部博客 (52)
- hibernate (3)
- spring (1)
- struts (0)
- commons (0)
- 英语自我介绍 (1)
- sql (18)
- extjs (0)
- linux (4)
- java (7)
- svn (0)
- html-css (1)
- ant (1)
- weblogic (1)
- perl (2)
- jsp (2)
- jms (2)
- log4j (1)
- android (2)
- maven (1)
- memcached (1)
- webservice (1)
- 在eclipse中使用Lombok (1)
- js (2)
- 在win7上安装oracle 10g配置文件更改 (0)
- myeclipse10安装svn (1)
最新评论
1 建立一个java工程 导入jms.jar wlfullclient5.jar
2 写几个类
package com.xmddl.jms.demo;
import java.io.Serializable;
public class UserInfo implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private String address;
private double age;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public double getAge() {
return age;
}
public void setAge(double age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.xmddl.jms.demo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Hashtable;
import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.StreamMessage;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class TopicSend {
// Defines the JNDI context factory.
public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";
// Defines the JNDI provider url.
public final static String PROVIDER_URL = "t3://localhost:7001";
// Defines the JMS connection factory for the queue.
public final static String JMS_FACTORY = "SendJMSFactory";
// Defines the queue.
public final static String TOPIC= "SendJMSTopic";
private TopicConnectionFactory qconFactory;
private TopicConnection qcon;
private TopicSession qsession;
private TopicPublisher publisher;
private Topic topic;
private TextMessage msg;
private StreamMessage sm;
private BytesMessage bm;
private MapMessage mm;
private ObjectMessage om;
public void init(Context ctx, String queueName) throws NamingException,
JMSException {
qconFactory = (TopicConnectionFactory) ctx.lookup(JMS_FACTORY);
qcon = qconFactory.createTopicConnection();
qsession = qcon.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
// qsession = qcon.createTopicSession(true, Session.AUTO_ACKNOWLEDGE);
topic = (Topic) ctx.lookup(queueName);
publisher = qsession.createPublisher(topic);
msg = qsession.createTextMessage();
sm=qsession.createStreamMessage();
bm=qsession.createBytesMessage();
mm=qsession.createMapMessage();
om=qsession.createObjectMessage();
qcon.start();
}
/**
* Sends a message to a JMS queue.
*
* @param message
* message to be sent
* @exception JMSException
* if JMS fails to send message due to internal error
*/
public void send(String message) throws JMSException {
//set TextMessage
msg.setText(message);
//set StreamMessage
sm.writeString("xmddl369");
sm.writeDouble(23.33);
//set BytesMessage
String name="xmddl369";
byte[] block=name.getBytes();
bm.writeBytes(block);
//set MapMessage
mm.setString("name", "xmddl369");
//set ObjectMessage
UserInfo ui=new UserInfo();
ui.setName("xmddl369");
ui.setAddress("beijing");
ui.setAge(100);
om.setObject(ui);
publisher.publish(msg);
// qsender.send(sm);
// qsender.send(bm);
// qsender.send(mm);
// publisher.publish(om);
// qsession.commit();
}
/**
* Closes JMS objects.
*
* @exception JMSException
* if JMS fails to close objects due to internal error
*/
public void close() throws JMSException {
publisher.close();
qsession.close();
qcon.close();
}
public static void main(String[] args) throws Exception {
InitialContext ic = getInitialContext();
TopicSend qs = new TopicSend();
qs.init(ic, TOPIC);
readAndSend(qs);
qs.close();
}
private static void readAndSend(TopicSend qs) throws IOException,
JMSException {
BufferedReader msgStream = new BufferedReader(new InputStreamReader(
System.in));
String line = null;
boolean quitNow = false;
do {
System.out.print("Enter message (\"quit\" to quit): ");
line = msgStream.readLine();
if (line != null && line.trim().length() != 0) {
qs.send(line);
System.out.println("JMS Message Sent: " + line + "\n");
quitNow = line.equalsIgnoreCase("quit");
}
} while (!quitNow);
}
private static InitialContext getInitialContext() throws NamingException {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, PROVIDER_URL);
return new InitialContext(env);
}
}
package com.xmddl.jms.demo;
import java.util.Hashtable;
import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.StreamMessage;
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;
import javax.naming.NamingException;
public class TopicReceive implements MessageListener{
// Defines the JNDI context factory.
public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";
// Defines the JNDI provider url.
public final static String PROVIDER_URL = "t3://localhost:7001";
// Defines the JMS connection factory for the topic.
public final static String JMS_FACTORY = "SendJMSFactory";
// Defines the topic.
public final static String TOPIC= "SendJMSTopic";
private TopicConnectionFactory qconFactory;
private TopicConnection qcon;
private TopicSession qsession;
private TopicSubscriber subscriber;
private Topic topic;
private TextMessage msg;
private boolean quit = false;
public void onMessage(Message msg) {
try {
String msgText = "";
double d = 0;
if (msg instanceof TextMessage) {
msgText = ((TextMessage) msg).getText();
} else if (msg instanceof StreamMessage) {
msgText = ((StreamMessage) msg).readString();
d = ((StreamMessage) msg).readDouble();
} else if (msg instanceof BytesMessage) {
byte[] block = new byte[1024];
((BytesMessage) msg).readBytes(block);
msgText = String.valueOf(block);
} else if (msg instanceof MapMessage) {
msgText = ((MapMessage) msg).getString("name");
} else if (msg instanceof ObjectMessage) {
UserInfo ui = (UserInfo) ((ObjectMessage) msg).getObject();
msgText = ui.getName();
d = ui.getAge();
}
System.out.println("Message Received: " + msgText + "\t" + d);
if (msgText.equalsIgnoreCase("quit")) {
synchronized (this) {
quit = true;
this.notifyAll(); // Notify main thread to quit
}
}
} catch (JMSException jmse) {
jmse.printStackTrace();
}
}
/**
* Creates all the necessary objects for receiving messages from a JMS
* queue.
*
* @param ctx
* JNDI initial context
* @param queueName
* name of queue
* @exception NamingException
* if operation cannot be performed
* @exception JMSException
* if JMS fails to initialize due to internal error
*/
public void init(Context ctx, String queueName) throws NamingException,
JMSException {
qconFactory = (TopicConnectionFactory) ctx.lookup(JMS_FACTORY);
qcon = qconFactory.createTopicConnection();
qsession = qcon.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
topic = (Topic) ctx.lookup(queueName);
subscriber = qsession.createSubscriber(topic);
subscriber.setMessageListener(this);
qcon.start();
}
/**
* Closes JMS objects.
*
* @exception JMSException
* if JMS fails to close objects due to internal error
*/
public void close() throws JMSException {
subscriber.close();
qsession.close();
qcon.close();
}
/**
* main() method.
*
* @param args
* WebLogic Server URL
* @exception Exception
* if execution fails
*/
public static void main(String[] args) throws Exception {
InitialContext ic = getInitialContext();
TopicReceive qr = new TopicReceive();
qr.init(ic, TOPIC);
System.out.println("JMS Ready To Receive Messages (To quit, send a \"quit\" message).");
// Wait until a "quit" message has been received.
synchronized (qr) {
while (!qr.quit) {
try {
qr.wait();
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
qr.close();
}
private static InitialContext getInitialContext() throws NamingException {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, PROVIDER_URL);
return new InitialContext(env);
}
public String getEncoding() throws Exception {
return "Hello World!";
}
}
package com.xmddl.jms.demo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Hashtable;
import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.StreamMessage;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class QueueSend {
// Defines the JNDI context factory.
public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";
// Defines the JNDI provider url.
public final static String PROVIDER_URL = "t3://localhost:7001";
// Defines the JMS connection factory for the queue.
public final static String JMS_FACTORY = "SendJMSFactory";
// Defines the queue.
public final static String QUEUE = "SendJMSQueue";
private QueueConnectionFactory qconFactory;
private QueueConnection qcon;
private QueueSession qsession;
private QueueSender qsender;
private Queue queue;
private TextMessage msg;
private StreamMessage sm;
private BytesMessage bm;
private MapMessage mm;
private ObjectMessage om;
/**
* Creates all the necessary objects for sending messages to a JMS queue.
*
* @param ctx
* JNDI initial context
* @param queueName
* name of queue
* @exception NamingException
* if operation cannot be performed
* @exception JMSException
* if JMS fails to initialize due to internal error
*/
public void init(Context ctx, String queueName) throws NamingException,
JMSException {
qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
qcon = qconFactory.createQueueConnection();
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Queue) ctx.lookup(queueName);
qsender = qsession.createSender(queue);
msg = qsession.createTextMessage();
sm = qsession.createStreamMessage();
bm = qsession.createBytesMessage();
mm = qsession.createMapMessage();
om = qsession.createObjectMessage();
qcon.start();
}
/**
* Sends a message to a JMS queue.
*
* @param message
* message to be sent
* @exception JMSException
* if JMS fails to send message due to internal error
*/
public void send(String message) throws JMSException {
// set TextMessage
msg.setText(message);
// set StreamMessage
sm.writeString("xmddl369");
sm.writeDouble(23.33);
// set BytesMessage
String name = "xmddl369";
byte[] block = name.getBytes();
bm.writeBytes(block);
// set MapMessage
mm.setString("name", "xmddl369");
// set ObjectMessage
UserInfo ui = new UserInfo();
ui.setName("xmddl369");
ui.setAddress("beijing");
ui.setAge(100);
om.setObject(ui);
qsender.send(msg);
// qsender.send(sm);
// qsender.send(bm);
// qsender.send(mm);
//qsender.send(om);
}
/**
* Closes JMS objects.
*
* @exception JMSException
* if JMS fails to close objects due to internal error
*/
public void close() throws JMSException {
qsender.close();
qsession.close();
qcon.close();
}
public static void main(String[] args) throws Exception {
InitialContext ic = getInitialContext();
QueueSend qs = new QueueSend();
qs.init(ic, QUEUE);
readAndSend(qs);
qs.close();
}
private static void readAndSend(QueueSend qs) throws IOException,
JMSException {
BufferedReader msgStream = new BufferedReader(new InputStreamReader(
System.in));
String line = null;
boolean quitNow = false;
do {
System.out.print("Enter message (\"quit\" to quit): ");
line = msgStream.readLine();
if (line != null && line.trim().length() != 0) {
qs.send(line);
System.out.println("JMS Message Sent: " + line + "\n");
quitNow = line.equalsIgnoreCase("quit");
}
} while (!quitNow);
}
private static InitialContext getInitialContext() throws NamingException {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, PROVIDER_URL);
return new InitialContext(env);
}
}
package com.xmddl.jms.demo;
import java.util.Hashtable;
import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.StreamMessage;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
/**
*
* @author Administrator
* <pre>
*
* </pre>
*/
public class QueueReceive implements MessageListener {
// Defines the JNDI context factory.
public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";
// Defines the JNDI provider url.
public final static String PROVIDER_URL = "t3://localhost:7001";
// Defines the JMS connection factory for the queue.
public final static String JMS_FACTORY = "SendJMSFactory";
// Defines the queue.
public final static String QUEUE = "SendJMSQueue";
private QueueConnectionFactory qconFactory;
private QueueConnection qcon;
private QueueSession qsession;
private QueueReceiver qreceiver;
private Queue queue;
private boolean quit = false;
/**
* Message listener interface.
*
* @param msg
* message
*/
public void onMessage(Message msg) {
try {
String msgText = "";
double d = 0;
if (msg instanceof TextMessage) {
msgText = ((TextMessage) msg).getText();
} else if (msg instanceof StreamMessage) {
msgText = ((StreamMessage) msg).readString();
d = ((StreamMessage) msg).readDouble();
} else if (msg instanceof BytesMessage) {
byte[] block = new byte[1024];
((BytesMessage) msg).readBytes(block);
msgText = String.valueOf(block);
} else if (msg instanceof MapMessage) {
msgText = ((MapMessage) msg).getString("name");
} else if (msg instanceof ObjectMessage) {
UserInfo ui = (UserInfo) ((ObjectMessage) msg).getObject();
msgText = ui.getName();
d = ui.getAge();
}
System.out.println("Message Received: " + msgText + "\t" + d);
if (msgText.equalsIgnoreCase("quit")) {
synchronized (this) {
quit = true;
this.notifyAll(); // Notify main thread to quit
}
}
} catch (JMSException jmse) {
jmse.printStackTrace();
}
}
/**
* Creates all the necessary objects for receiving messages from a JMS
* queue.
*
* @param ctx
* JNDI initial context
* @param queueName
* name of queue
* @exception NamingException
* if operation cannot be performed
* @exception JMSException
* if JMS fails to initialize due to internal error
*/
public void init(Context ctx, String queueName) throws NamingException,
JMSException {
qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
qcon = qconFactory.createQueueConnection();
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Queue) ctx.lookup(queueName);
qreceiver = qsession.createReceiver(queue);
qreceiver.setMessageListener(this);
qcon.start();
}
/**
* Closes JMS objects.
*
* @exception JMSException
* if JMS fails to close objects due to internal error
*/
public void close() throws JMSException {
qreceiver.close();
qsession.close();
qcon.close();
}
/**
* main() method.
*
* @param args
* WebLogic Server URL
* @exception Exception
* if execution fails
*/
public static void main(String[] args) throws Exception {
InitialContext ic = getInitialContext();
QueueReceive qr = new QueueReceive();
qr.init(ic, QUEUE);
System.out
.println("JMS Ready To Receive Messages (To quit, send a \"quit\" message).");
// Wait until a "quit" message has been received.
synchronized (qr) {
while (!qr.quit) {
try {
qr.wait();
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
qr.close();
}
private static InitialContext getInitialContext() throws NamingException {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, PROVIDER_URL);
return new InitialContext(env);
}
public String getEncoding() throws Exception {
return "Hello World!";
}
}
3.在weblogic10.3上的console配置 connectionFactory,queue,topic
4.测试运行
2 写几个类
package com.xmddl.jms.demo;
import java.io.Serializable;
public class UserInfo implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private String address;
private double age;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public double getAge() {
return age;
}
public void setAge(double age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.xmddl.jms.demo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Hashtable;
import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.StreamMessage;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class TopicSend {
// Defines the JNDI context factory.
public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";
// Defines the JNDI provider url.
public final static String PROVIDER_URL = "t3://localhost:7001";
// Defines the JMS connection factory for the queue.
public final static String JMS_FACTORY = "SendJMSFactory";
// Defines the queue.
public final static String TOPIC= "SendJMSTopic";
private TopicConnectionFactory qconFactory;
private TopicConnection qcon;
private TopicSession qsession;
private TopicPublisher publisher;
private Topic topic;
private TextMessage msg;
private StreamMessage sm;
private BytesMessage bm;
private MapMessage mm;
private ObjectMessage om;
public void init(Context ctx, String queueName) throws NamingException,
JMSException {
qconFactory = (TopicConnectionFactory) ctx.lookup(JMS_FACTORY);
qcon = qconFactory.createTopicConnection();
qsession = qcon.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
// qsession = qcon.createTopicSession(true, Session.AUTO_ACKNOWLEDGE);
topic = (Topic) ctx.lookup(queueName);
publisher = qsession.createPublisher(topic);
msg = qsession.createTextMessage();
sm=qsession.createStreamMessage();
bm=qsession.createBytesMessage();
mm=qsession.createMapMessage();
om=qsession.createObjectMessage();
qcon.start();
}
/**
* Sends a message to a JMS queue.
*
* @param message
* message to be sent
* @exception JMSException
* if JMS fails to send message due to internal error
*/
public void send(String message) throws JMSException {
//set TextMessage
msg.setText(message);
//set StreamMessage
sm.writeString("xmddl369");
sm.writeDouble(23.33);
//set BytesMessage
String name="xmddl369";
byte[] block=name.getBytes();
bm.writeBytes(block);
//set MapMessage
mm.setString("name", "xmddl369");
//set ObjectMessage
UserInfo ui=new UserInfo();
ui.setName("xmddl369");
ui.setAddress("beijing");
ui.setAge(100);
om.setObject(ui);
publisher.publish(msg);
// qsender.send(sm);
// qsender.send(bm);
// qsender.send(mm);
// publisher.publish(om);
// qsession.commit();
}
/**
* Closes JMS objects.
*
* @exception JMSException
* if JMS fails to close objects due to internal error
*/
public void close() throws JMSException {
publisher.close();
qsession.close();
qcon.close();
}
public static void main(String[] args) throws Exception {
InitialContext ic = getInitialContext();
TopicSend qs = new TopicSend();
qs.init(ic, TOPIC);
readAndSend(qs);
qs.close();
}
private static void readAndSend(TopicSend qs) throws IOException,
JMSException {
BufferedReader msgStream = new BufferedReader(new InputStreamReader(
System.in));
String line = null;
boolean quitNow = false;
do {
System.out.print("Enter message (\"quit\" to quit): ");
line = msgStream.readLine();
if (line != null && line.trim().length() != 0) {
qs.send(line);
System.out.println("JMS Message Sent: " + line + "\n");
quitNow = line.equalsIgnoreCase("quit");
}
} while (!quitNow);
}
private static InitialContext getInitialContext() throws NamingException {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, PROVIDER_URL);
return new InitialContext(env);
}
}
package com.xmddl.jms.demo;
import java.util.Hashtable;
import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.StreamMessage;
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;
import javax.naming.NamingException;
public class TopicReceive implements MessageListener{
// Defines the JNDI context factory.
public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";
// Defines the JNDI provider url.
public final static String PROVIDER_URL = "t3://localhost:7001";
// Defines the JMS connection factory for the topic.
public final static String JMS_FACTORY = "SendJMSFactory";
// Defines the topic.
public final static String TOPIC= "SendJMSTopic";
private TopicConnectionFactory qconFactory;
private TopicConnection qcon;
private TopicSession qsession;
private TopicSubscriber subscriber;
private Topic topic;
private TextMessage msg;
private boolean quit = false;
public void onMessage(Message msg) {
try {
String msgText = "";
double d = 0;
if (msg instanceof TextMessage) {
msgText = ((TextMessage) msg).getText();
} else if (msg instanceof StreamMessage) {
msgText = ((StreamMessage) msg).readString();
d = ((StreamMessage) msg).readDouble();
} else if (msg instanceof BytesMessage) {
byte[] block = new byte[1024];
((BytesMessage) msg).readBytes(block);
msgText = String.valueOf(block);
} else if (msg instanceof MapMessage) {
msgText = ((MapMessage) msg).getString("name");
} else if (msg instanceof ObjectMessage) {
UserInfo ui = (UserInfo) ((ObjectMessage) msg).getObject();
msgText = ui.getName();
d = ui.getAge();
}
System.out.println("Message Received: " + msgText + "\t" + d);
if (msgText.equalsIgnoreCase("quit")) {
synchronized (this) {
quit = true;
this.notifyAll(); // Notify main thread to quit
}
}
} catch (JMSException jmse) {
jmse.printStackTrace();
}
}
/**
* Creates all the necessary objects for receiving messages from a JMS
* queue.
*
* @param ctx
* JNDI initial context
* @param queueName
* name of queue
* @exception NamingException
* if operation cannot be performed
* @exception JMSException
* if JMS fails to initialize due to internal error
*/
public void init(Context ctx, String queueName) throws NamingException,
JMSException {
qconFactory = (TopicConnectionFactory) ctx.lookup(JMS_FACTORY);
qcon = qconFactory.createTopicConnection();
qsession = qcon.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
topic = (Topic) ctx.lookup(queueName);
subscriber = qsession.createSubscriber(topic);
subscriber.setMessageListener(this);
qcon.start();
}
/**
* Closes JMS objects.
*
* @exception JMSException
* if JMS fails to close objects due to internal error
*/
public void close() throws JMSException {
subscriber.close();
qsession.close();
qcon.close();
}
/**
* main() method.
*
* @param args
* WebLogic Server URL
* @exception Exception
* if execution fails
*/
public static void main(String[] args) throws Exception {
InitialContext ic = getInitialContext();
TopicReceive qr = new TopicReceive();
qr.init(ic, TOPIC);
System.out.println("JMS Ready To Receive Messages (To quit, send a \"quit\" message).");
// Wait until a "quit" message has been received.
synchronized (qr) {
while (!qr.quit) {
try {
qr.wait();
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
qr.close();
}
private static InitialContext getInitialContext() throws NamingException {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, PROVIDER_URL);
return new InitialContext(env);
}
public String getEncoding() throws Exception {
return "Hello World!";
}
}
package com.xmddl.jms.demo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Hashtable;
import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.StreamMessage;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class QueueSend {
// Defines the JNDI context factory.
public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";
// Defines the JNDI provider url.
public final static String PROVIDER_URL = "t3://localhost:7001";
// Defines the JMS connection factory for the queue.
public final static String JMS_FACTORY = "SendJMSFactory";
// Defines the queue.
public final static String QUEUE = "SendJMSQueue";
private QueueConnectionFactory qconFactory;
private QueueConnection qcon;
private QueueSession qsession;
private QueueSender qsender;
private Queue queue;
private TextMessage msg;
private StreamMessage sm;
private BytesMessage bm;
private MapMessage mm;
private ObjectMessage om;
/**
* Creates all the necessary objects for sending messages to a JMS queue.
*
* @param ctx
* JNDI initial context
* @param queueName
* name of queue
* @exception NamingException
* if operation cannot be performed
* @exception JMSException
* if JMS fails to initialize due to internal error
*/
public void init(Context ctx, String queueName) throws NamingException,
JMSException {
qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
qcon = qconFactory.createQueueConnection();
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Queue) ctx.lookup(queueName);
qsender = qsession.createSender(queue);
msg = qsession.createTextMessage();
sm = qsession.createStreamMessage();
bm = qsession.createBytesMessage();
mm = qsession.createMapMessage();
om = qsession.createObjectMessage();
qcon.start();
}
/**
* Sends a message to a JMS queue.
*
* @param message
* message to be sent
* @exception JMSException
* if JMS fails to send message due to internal error
*/
public void send(String message) throws JMSException {
// set TextMessage
msg.setText(message);
// set StreamMessage
sm.writeString("xmddl369");
sm.writeDouble(23.33);
// set BytesMessage
String name = "xmddl369";
byte[] block = name.getBytes();
bm.writeBytes(block);
// set MapMessage
mm.setString("name", "xmddl369");
// set ObjectMessage
UserInfo ui = new UserInfo();
ui.setName("xmddl369");
ui.setAddress("beijing");
ui.setAge(100);
om.setObject(ui);
qsender.send(msg);
// qsender.send(sm);
// qsender.send(bm);
// qsender.send(mm);
//qsender.send(om);
}
/**
* Closes JMS objects.
*
* @exception JMSException
* if JMS fails to close objects due to internal error
*/
public void close() throws JMSException {
qsender.close();
qsession.close();
qcon.close();
}
public static void main(String[] args) throws Exception {
InitialContext ic = getInitialContext();
QueueSend qs = new QueueSend();
qs.init(ic, QUEUE);
readAndSend(qs);
qs.close();
}
private static void readAndSend(QueueSend qs) throws IOException,
JMSException {
BufferedReader msgStream = new BufferedReader(new InputStreamReader(
System.in));
String line = null;
boolean quitNow = false;
do {
System.out.print("Enter message (\"quit\" to quit): ");
line = msgStream.readLine();
if (line != null && line.trim().length() != 0) {
qs.send(line);
System.out.println("JMS Message Sent: " + line + "\n");
quitNow = line.equalsIgnoreCase("quit");
}
} while (!quitNow);
}
private static InitialContext getInitialContext() throws NamingException {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, PROVIDER_URL);
return new InitialContext(env);
}
}
package com.xmddl.jms.demo;
import java.util.Hashtable;
import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.StreamMessage;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
/**
*
* @author Administrator
* <pre>
*
* </pre>
*/
public class QueueReceive implements MessageListener {
// Defines the JNDI context factory.
public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";
// Defines the JNDI provider url.
public final static String PROVIDER_URL = "t3://localhost:7001";
// Defines the JMS connection factory for the queue.
public final static String JMS_FACTORY = "SendJMSFactory";
// Defines the queue.
public final static String QUEUE = "SendJMSQueue";
private QueueConnectionFactory qconFactory;
private QueueConnection qcon;
private QueueSession qsession;
private QueueReceiver qreceiver;
private Queue queue;
private boolean quit = false;
/**
* Message listener interface.
*
* @param msg
* message
*/
public void onMessage(Message msg) {
try {
String msgText = "";
double d = 0;
if (msg instanceof TextMessage) {
msgText = ((TextMessage) msg).getText();
} else if (msg instanceof StreamMessage) {
msgText = ((StreamMessage) msg).readString();
d = ((StreamMessage) msg).readDouble();
} else if (msg instanceof BytesMessage) {
byte[] block = new byte[1024];
((BytesMessage) msg).readBytes(block);
msgText = String.valueOf(block);
} else if (msg instanceof MapMessage) {
msgText = ((MapMessage) msg).getString("name");
} else if (msg instanceof ObjectMessage) {
UserInfo ui = (UserInfo) ((ObjectMessage) msg).getObject();
msgText = ui.getName();
d = ui.getAge();
}
System.out.println("Message Received: " + msgText + "\t" + d);
if (msgText.equalsIgnoreCase("quit")) {
synchronized (this) {
quit = true;
this.notifyAll(); // Notify main thread to quit
}
}
} catch (JMSException jmse) {
jmse.printStackTrace();
}
}
/**
* Creates all the necessary objects for receiving messages from a JMS
* queue.
*
* @param ctx
* JNDI initial context
* @param queueName
* name of queue
* @exception NamingException
* if operation cannot be performed
* @exception JMSException
* if JMS fails to initialize due to internal error
*/
public void init(Context ctx, String queueName) throws NamingException,
JMSException {
qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
qcon = qconFactory.createQueueConnection();
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Queue) ctx.lookup(queueName);
qreceiver = qsession.createReceiver(queue);
qreceiver.setMessageListener(this);
qcon.start();
}
/**
* Closes JMS objects.
*
* @exception JMSException
* if JMS fails to close objects due to internal error
*/
public void close() throws JMSException {
qreceiver.close();
qsession.close();
qcon.close();
}
/**
* main() method.
*
* @param args
* WebLogic Server URL
* @exception Exception
* if execution fails
*/
public static void main(String[] args) throws Exception {
InitialContext ic = getInitialContext();
QueueReceive qr = new QueueReceive();
qr.init(ic, QUEUE);
System.out
.println("JMS Ready To Receive Messages (To quit, send a \"quit\" message).");
// Wait until a "quit" message has been received.
synchronized (qr) {
while (!qr.quit) {
try {
qr.wait();
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
qr.close();
}
private static InitialContext getInitialContext() throws NamingException {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
env.put(Context.PROVIDER_URL, PROVIDER_URL);
return new InitialContext(env);
}
public String getEncoding() throws Exception {
return "Hello World!";
}
}
3.在weblogic10.3上的console配置 connectionFactory,queue,topic
4.测试运行
相关推荐
总结来说,"activeMQ-JMS实例"是一个基于Spring MVC的示例,它演示了如何使用Spring的JMS支持与ActiveMQ结合,实现消息的发送和接收。这个实例对于学习和理解JMS以及如何在实际项目中应用它是很有帮助的。通过这个...
JMS实例代码通常会涉及到异常处理,如JMSException和其他相关的运行时异常,确保在出现问题时能够优雅地处理错误。此外,为了提高性能和可用性,可以使用事务管理(Session的TRANSACTED或CLIENT_ACKNOWLEDGE模式)...
【JBoss 服务器下的 JMS 实例】 Java 消息服务 (JMS) 是 Java 平台中用于处理异步消息传递的标准API,它在面向服务架构 (SOA) 中扮演着关键角色,特别是在需要与外部系统进行异步通信的企业环境中。JBoss 服务器...
本实例中,"JMS实例,整合spring,含jar"表明这是一个具体的项目示例,展示了如何在Spring框架中配置和使用JMS。这通常涉及到以下步骤: 1. **添加依赖**:首先,你需要在项目的构建文件(如Maven的pom.xml或Gradle的...
通过以上的JMS实例源码,我们可以看到如何在Java程序中创建、发送和接收消息,以及如何选择适合的通信模型(点对点或发布/订阅)和通信方式(同步或异步)。在实际开发中,根据业务需求选择合适的模型和通信方式,...
**JMS完全实例详解** Java消息服务(Java Message Service,简称JMS)是Java平台中用于企业级应用间异步通信的一种标准接口。它允许应用程序创建、发送、接收和读取消息,为分布式系统提供了可靠的通信机制。在这个...
Java Message Service (JMS) 是Java平台中用于在分布式环境中交换异步消息的标准API。它为应用程序提供了一种可靠和高效的方式来发送、接收和管理消息,这些消息可以在不同的应用程序之间传输,甚至跨越不同的网络和...
description="JMS Connection Factory" factory="org.apache.activemq.jndi.JNDIReferenceFactory" brokerURL="vm://shirdrnUrl" brokerName="MyActiveMQBroker"/> auth="Container" type="org....
在实现JMS实例时,你可以采用编程模型或声明式模型。编程模型需要在代码中显式创建和管理JMS资源,而声明式模型通过WebLogic管理控制台或XML配置文件完成资源配置。 例如,一个简单的JMS生产者可能会创建一个消息,...
本文将深入探讨一个基于WebSphere MQ的简单Java Message Service (JMS) 实例,帮助你理解如何在实际项目中实现消息的发送。 首先,我们需要了解JMS。JMS是Java平台上的一个标准接口,它定义了用于在分布式环境中...
Spring 整合 Weblogic JMS 实例详解 Spring 整合 Weblogic JMS 实例详解是指在 Spring 框架下整合 Weblogic JMS 服务器,以便在应用程序中使用 JMS messaging。这种整合方式可以提供高性能、可靠性和灵活性的消息...
在代码实现中,通常会创建一个`ConnectionFactory`实例,通过它创建一个`Connection`,然后从`Connection`创建一个`Session`,最后在`Session`上创建`MessageProducer`。 2. **消息消费者**:消息消费者是接收和...
Java jms处理消息请求的例子,Java创建和初始化一个请求Requestor对象,初始化请求Requestor对象,接受由反馈者replier发回的消息,响应request产生要输出的信息,输出响应信息。
【JBOSST建立JMS应用实例】 JBOSST(Java Business Open Source Solutions)是一个开源的企业级应用服务器,它提供了全面的中间件服务,包括对Java消息服务(JMS)的支持。JMS是一种标准,用于在分布式环境中传递...
在"jms实例"中,你可能会涉及以下知识点: 1. JMS接口和实现:JMS API定义了多种接口,如`ConnectionFactory`、`MessageProducer`、`MessageConsumer`、`Message`等,开发者需要选择一个JMS提供商(如ActiveMQ、...
JMS 详细实例学习教程 JMS(Java Message Service)是一种基于Java平台的消息服务规范,用于在分布式系统中异步通信。JMS提供了一种灵活、可靠、可扩展的消息传递机制,使得应用程序之间可以相互通信和交换数据。 ...
三个jms的实例,实现了jms的queue和topic,同时也实现了远程传送消息和本地传送消息,另外也分别实现了session的事务和非事务模式,有了这三个例子应该可以满足你的任何关于jms的需求了,由于上传限制,另外还需要...
**JMS整合Spring实例** Java消息服务(Java Message Service,简称JMS)是Java平台中用于企业级应用间异步通信的一种标准接口。它允许应用程序创建、发送、接收和读取消息,以此来解耦生产者和消费者。而Spring框架...
JMS P2P 实例程序 希望给大家版主