- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
大家好,请为新手提供一些关于带有 JMS 的 ActiveMQ 的基础知识。而且配置步骤也一样。
最佳答案
我们将使用多线程创建一个基于控制台的应用程序。所以为控制台应用程序创建一个java项目。
现在按照这些步骤......
# START SNIPPET: jndi
java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory
# use the following property to configure the default connector
java.naming.provider.url = tcp://localhost:61616
# use the following property to specify the JNDI name the connection factory
# should appear as.
#connectionFactoryNames = connectionFactory, queueConnectionFactory, topicConnectionFactry
connectionFactoryNames = connectionFactory, queueConnectionFactory, topicConnectionFactry
# register some queues in JNDI using the form
# queue.[jndiName] = [physicalName]
queue.MyQueue = example.MyQueue
# register some topics in JNDI using the form
# topic.[jndiName] = [physicalName]
topic.MyTopic = example.MyTopic
# END SNIPPET: jndi
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class JMSConsumer implements Runnable{
private static final Log LOG = LogFactory.getLog(JMSConsumer.class);
public void run() {
Context jndiContext = null;
ConnectionFactory connectionFactory = null;
Connection connection = null;
Session session = null;
MessageConsumer consumer = null;
Destination destination = null;
String sourceName = null;
final int numMsgs;
sourceName= "MyQueue";
numMsgs = 1;
LOG.info("Source name is " + sourceName);
/*
* Create a JNDI API InitialContext object
*/
try {
jndiContext = new InitialContext();
} catch (NamingException e) {
LOG.info("Could not create JNDI API context: " + e.toString());
System.exit(1);
}
/*
* Look up connection factory and destination.
*/
try {
connectionFactory = (ConnectionFactory)jndiContext.lookup("queueConnectionFactory");
destination = (Destination)jndiContext.lookup(sourceName);
} catch (NamingException e) {
LOG.info("JNDI API lookup failed: " + e);
System.exit(1);
}
try {
connection = connectionFactory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
consumer = session.createConsumer(destination);
connection.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
MessageListener listener = new MyQueueMessageListener();
consumer.setMessageListener(listener );
//Let the thread run for some time so that the Consumer has suffcient time to consume the message
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (JMSException e) {
LOG.info("Exception occurred: " + e);
} finally {
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
}
}
}
}
}
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class JMSProducer implements Runnable{
private static final Log LOG = LogFactory.getLog(JMSProducer.class);
public JMSProducer() {
}
//Run method implemented to run this as a thread.
public void run(){
Context jndiContext = null;
ConnectionFactory connectionFactory = null;
Connection connection = null;
Session session = null;
Destination destination = null;
MessageProducer producer = null;
String destinationName = null;
final int numMsgs;
destinationName = "MyQueue";
numMsgs = 5;
LOG.info("Destination name is " + destinationName);
/*
* Create a JNDI API InitialContext object
*/
try {
jndiContext = new InitialContext();
} catch (NamingException e) {
LOG.info("Could not create JNDI API context: " + e.toString());
System.exit(1);
}
/*
* Look up connection factory and destination.
*/
try {
connectionFactory = (ConnectionFactory)jndiContext.lookup("queueConnectionFactory");
destination = (Destination)jndiContext.lookup(destinationName);
} catch (NamingException e) {
LOG.info("JNDI API lookup failed: " + e);
System.exit(1);
}
/*
* Create connection. Create session from connection; false means
* session is not transacted.create producer, set the text message, set the co-relation id and send the message.
*/
try {
connection = connectionFactory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(destination);
TextMessage message = session.createTextMessage();
for (int i = 0; i
Add MyQueueMessageListener.java
import java.io.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.jms.*;
public class MyQueueMessageListener implements MessageListener {
private static final Log LOG = LogFactory.getLog(MyQueueMessageListener.class);
/**
*
*/
public MyQueueMessageListener() {
// TODO Auto-generated constructor stub
}
/** (non-Javadoc)
* @see javax.jms.MessageListener#onMessage(javax.jms.Message)
* This is called on receving of a text message.
*/
public void onMessage(Message arg0) {
LOG.info("onMessage() called!");
if(arg0 instanceof TextMessage){
try {
//Print it out
System.out.println("Recieved message in listener: " + ((TextMessage)arg0).getText());
System.out.println("Co-Rel Id: " + ((TextMessage)arg0).getJMSCorrelationID());
try {
//Log it to a file
BufferedWriter outFile = new BufferedWriter(new FileWriter("MyQueueConsumer.txt"));
outFile.write("Recieved message in listener: " + ((TextMessage)arg0).getText());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
System.out.println("~~~~Listener : Error in message format~~~~");
}
}
}
Add SimpleApp.java
public class SimpleApp {
//Run the producer first, then the consumer
public static void main(String[] args) throws Exception {
runInNewthread(new JMSProducer());
runInNewthread(new JMSConsumer());
}
public static void runInNewthread(Runnable runnable) {
Thread brokerThread = new Thread(runnable);
brokerThread.setDaemon(false);
brokerThread.start();
}
}
关于ActiveMQ 和 JMS : Basic steps for novice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4516113/
在我用 OpenCascade 生成的 STEP 文件中,我可以看到: #58 = VERTEX_POINT('',#59); #59 = CARTESIAN_POINT('',(-0.5,-0.5,
这个问题在这里已经有了答案: How to use Firebug's debugger functions? (1 个回答) 关闭 8 年前。 我是 FireBug 调试器的新手,谁能告诉我什么是
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我想知道 Eclipse IDE 中是否存在任何功能,可以在执行 selenium 脚本 时“从步骤进行调试” “单步执行”、“单步执行”。 “QTP” 和大多数专有功能自动化工具中都提供此功能。我假
我正在寻找 .stp/.step 文件的某种规范、文档、解释等。 更多的是关于每行包含哪些信息而不是一般信息。 我似乎无法自己弄清楚每个值的含义。 有谁知道一些关于 STEP 文件的好读物? 我已经搜
我写了下面的代码: Mockup TOPdesk
我正在尝试研究 java String 类中的方法如何工作,因此我创建了一些调用该 String 类方法的自定义代码。 如您所见,我在自己的代码中设置了一个断点,并且在 java String 类源代
我有某些功能,需要我进行一个 api 调用来检索第二个 api 调用中使用的某个 id,逻辑上它看起来像这样: componentWillMount () { this.props.functio
在本地使用 AppEngine(即使用 dev_appserver.py)时,是否可以进行逐步调试?使用 logging.info() 或类似的函数来显示代码中所有变量的值并确定错误的位置是一种过时的
我正在使用 JQuery Steps,它非常好。但事实证明有些事情很棘手。 假设我在第 2 步。根据我在表单中选择的内容,我可能想在单击“下一步”时直接跳到第 4 步,并且进一步禁用第 3 步 anc
我已经包含了 jquery-steps 插件。 如何更改按钮文本? 现在它说“完成”我想把它改成“去” 谢谢 最佳答案 查看以下内容 link .您可以在初始化时更改所有标签。 var setting
在(现已弃用的) Angular 场景测试运行器中,有一个选项可以创建一个 runner.html 页面,该页面将在 iFrame 中运行测试,同时在主页中逐步报告进度。 有没有办法获得类似的 Pro
我有使用标签 的 primefaces 步骤像下面这样: 结果是这样的: 我可以点击步骤 1,但不能点击步骤 3 和 4。如何为所有步骤启用点击? 最佳答案 哇,
给定一个使用分区的 Spring Batch 作业,是否可以有多个分区步骤? 例如: 在上面的示例中,是否可以将另一个分区
假设您要从数据库中获取一条记录,该记录返回大量数据并且需要多次连接。 所以我的问题是,使用单个查询来检查数据是否存在并在存在时获取结果是否更好?或者做一个更简单的查询,检查数据是否存在然后id记录是否
我如何创建一个具有整数输入类型而不是字符串的任务,例如我想启动一个状态机: "SomeStage": { "Comment": "Getting busy
我还没有看到有人问过这个问题——尽管我确实阅读了大约 100 个关于类似主题的 jQuery 步骤——似乎没有一个能解决我的问题。 我正在使用 jQuery-steps 并希望在第一步完成后添加一个“
我想知道是否可以在 Laravel 5.3 中逐个迁移。 我无法使 --step 选项正常工作,文档仅提及此选项与 rollback 或 refresh 一起使用命令。 // this works p
我已按照 AWS 文档中的步骤在本地设置和运行 AWS Step Functions:https://docs.aws.amazon.com/step-functions/latest/dg/sfn-
我已按照 AWS 文档中的步骤在本地设置和运行 AWS Step Functions:https://docs.aws.amazon.com/step-functions/latest/dg/sfn-
我是一名优秀的程序员,十分优秀!