请教java怎么向activemq发送消息

 我来答
在晴天的雨伞
2016-04-26 · TA获得超过6869个赞
知道大有可为答主
回答量:5761
采纳率:86%
帮助的人:1213万
展开全部
一.Prop类(用来读取属性文件,单例)

package com.sitinspring.standardWeblogicJms;

import java.io.FileInputStream;
import java.util.Hashtable;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;

public class Props {
private static final String File_Name = "jmsSetting.properties";

private static Properties propts;

public static void makeProptsInstance() {
propts = new Properties();

try {
propts.load(new FileInputStream(File_Name));
} catch (Exception ex) {
ex.printStackTrace();
}
}

public static String get(String name){
if(propts==null){
makeProptsInstance();
}

return (String)propts.get(name);
}

@SuppressWarnings("unchecked")
public static Context getInitialContext(){
Context context=null;

String jndiFactory=Props.get("jndi.factory");
String providerUrl=Props.get("jndi.provider.url");

Hashtable env=new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, jndiFactory);
env.put(Context.PROVIDER_URL, providerUrl);

try {
context=new InitialContext(env);
} catch (Exception ex) {
ex.printStackTrace();
}

return context;
}
}
二.QueueBase类(QueueComsumer和QueueSupplier的基类,用于归纳一些两类共通的东西)
package com.sitinspring.standardWeblogicJms;

import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.naming.Context;

public class QueueBase{
protected QueueConnectionFactory queueConnectionFactory;
protected QueueConnection queueConnection;
protected QueueSession queueSession;
protected Queue queue;

public QueueBase(Context context){
try{
String jmsFactory=Props.get("jms.factory");
queueConnectionFactory=(QueueConnectionFactory)context.lookup(jmsFactory);
queueConnection=queueConnectionFactory.createQueueConnection();
queueSession=queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

String queueName=Props.get("queue.name");
queue=(Queue)context.lookup(queueName);
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
三.QueueComsumer类(用于接收消息)
package com.sitinspring.standardWeblogicJms;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.QueueReceiver;
import javax.jms.TextMessage;
import javax.naming.Context;

public class QueueComsumer extends QueueBase implements MessageListener {
private QueueReceiver queueReceiver;

public QueueComsumer(Context context) {
super(context);

try {
queueReceiver = queueSession.createReceiver(queue);
queueReceiver.setMessageListener(this);
queueConnection.start();
} catch (Exception ex) {
ex.printStackTrace();
}
}

public void onMessage(Message message) {
if (message instanceof TextMessage) {
TextMessage txtmsg = (TextMessage) message;

try {
System.out.println("I have received the TextMassage:"
+ txtmsg.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}

public void close() throws JMSException {
queueReceiver.close();
queueSession.close();
queueConnection.close();
}

public static void main(String[] args) {
Context context = Props.getInitialContext();
QueueComsumer queueComsumer = new QueueComsumer(context);

synchronized (queueComsumer) {
while (true) {
try {
queueComsumer.wait();
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();

try {
queueComsumer.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
}
}
四.QueueSupplier类(用于发送消息)
package com.sitinspring.standardWeblogicJms;

import javax.jms.JMSException;
import javax.jms.QueueSender;
import javax.jms.TextMessage;
import javax.naming.Context;

public class QueueSupplier extends QueueBase {
private QueueSender queueSender;

public QueueSupplier(Context context) {
super(context);

try {
queueSender = queueSession.createSender(queue);
} catch (Exception ex) {
ex.printStackTrace();
}
}

public void sendMsg(String msg) throws JMSException {
TextMessage txtMsg = queueSession.createTextMessage();
txtMsg.setText(msg);

queueConnection.start();
queueSender.send(txtMsg);
}

public void close() throws JMSException {
queueSender.close();
queueSession.close();
queueConnection.close();
}

public static void main(String[] args) {
Context context = Props.getInitialContext();
QueueSupplier queueSupplier = new QueueSupplier(context);

try {
queueSupplier.sendMsg("Hello World");

System.out.println("A message have been sent!");
} catch (JMSException ex) {
ex.printStackTrace();
} finally {
try {
queueSupplier.close();
} catch (JMSException ex) {
ex.printStackTrace();
}
}
}
}
网易云信
2023-12-06 广告
很高兴能回答您的问题。以下是一段针对“一对一消息组件”的描述,字数在200字左右:该组件支持用户之间的私密交流,让沟通更加直接和高效。通过它,您可以向特定对象发送消息,并实时查看消息状态,包括对方是否已读或未读。同时,该组件还支持富文本消息... 点击进入详情页
本回答由网易云信提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式