ARTEMIS-1021 Improving CPP to produce a TextMessage using UTF-8

This commit is contained in:
Clebert Suconic 2017-03-09 11:05:16 -05:00 committed by Justin Bertram
parent 101c6e6860
commit 1ece044dfc
3 changed files with 53 additions and 59 deletions

View File

@ -45,6 +45,11 @@ under the License.
<groupId>org.apache.geronimo.specs</groupId> <groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_2.0_spec</artifactId> <artifactId>geronimo-jms_2.0_spec</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.apache.qpid</groupId>
<artifactId>qpid-jms-client</artifactId>
<version>${qpid.jms.version}</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -33,7 +33,7 @@ using namespace qpid::messaging;
int main(int argc, char** argv) { int main(int argc, char** argv) {
std::string broker = argc > 1 ? argv[1] : "localhost:61616"; std::string broker = argc > 1 ? argv[1] : "localhost:61616";
std::string address = argc > 2 ? argv[2] : "jms.queue.exampleQueue"; std::string address = argc > 2 ? argv[2] : "exampleQueue";
// Connection options documented at http://qpid.apache.org/releases/qpid-0.30/programming/book/connections.html#connection-options // Connection options documented at http://qpid.apache.org/releases/qpid-0.30/programming/book/connections.html#connection-options
std::string connectionOptions = argc > 3 ? argv[3] : "{protocol:amqp1.0}"; std::string connectionOptions = argc > 3 ? argv[3] : "{protocol:amqp1.0}";
@ -49,20 +49,28 @@ int main(int argc, char** argv) {
// Step 5. Create a sender // Step 5. Create a sender
Sender sender = session.createSender(address); Sender sender = session.createSender(address);
// Step 6. send a simple message //create a receiver
sender.send(Message("Hello world!"));
// Step 7. create a receiver
Receiver receiver = session.createReceiver(address); Receiver receiver = session.createReceiver(address);
// Step 8. receive the simple message
Message message = receiver.fetch(Duration::SECOND * 1);
std::cout << "Received a message with this following content \"" << message.getContent() << "\"" << std::endl;
// Step 9. acknowledge the message for (int i = 0; i < 10; i++) {
session.acknowledge(); Message message;
message.getContentObject() = "Hello world!";
// Step 10. close the connection message.getContentObject().setEncoding("utf8");
message.setContentType("text/plain");
sender.send(message);
// receive the simple message
message = receiver.fetch(Duration::SECOND * 1);
std::cout << "Received a message with this following content \"" << message.getContent() << "\"" << std::endl;
// acknowledge the message
session.acknowledge();
}
// close the connection
connection.close(); connection.close();
return 0; return 0;
} catch(const std::exception& error) { } catch(const std::exception& error) {

View File

@ -18,24 +18,19 @@ package org.apache.activemq.artemis.jms.example;
import javax.jms.Connection; import javax.jms.Connection;
import javax.jms.ConnectionFactory; import javax.jms.ConnectionFactory;
import javax.jms.Message;
import javax.jms.MessageConsumer; import javax.jms.MessageConsumer;
import javax.jms.MessageProducer; import javax.jms.MessageProducer;
import javax.jms.Queue; import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueRequestor;
import javax.jms.QueueSession;
import javax.jms.Session; import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext; import javax.naming.InitialContext;
import org.apache.activemq.artemis.api.jms.ActiveMQJMSClient; import org.apache.qpid.jms.JmsConnectionFactory;
import org.apache.activemq.artemis.api.jms.management.JMSManagementHelper;
import org.apache.activemq.artemis.api.core.management.ResourceNames;
/** /**
* This example demonstrates the use of ActiveMQ Artemis "pre-acknowledge" functionality where * This example demonstrates the use of ActiveMQ Artemis "pre-acknowledge" functionality where
* messages are acknowledged before they are delivered to the consumer. * messages are acknowledged before they are delivered to the consumer.
* * <p>
* Please see the readme.html for more details. * Please see the readme.html for more details.
*/ */
public class ProtonCPPExample { public class ProtonCPPExample {
@ -45,24 +40,26 @@ public class ProtonCPPExample {
InitialContext initialContext = null; InitialContext initialContext = null;
try { try {
// Step 1. Create an initial context to perform the JNDI lookup. // Create an initial context to perform the JNDI lookup.
initialContext = new InitialContext(); initialContext = new InitialContext();
// Step 2. Perform the look-ups // if you wanted to use Core JMS, use this line instead.
Queue queue = (Queue) initialContext.lookup("queue/exampleQueue"); // ConnectionFactory cf = new ActiveMQConnectionFactory();
ConnectionFactory cf = new JmsConnectionFactory("amqp://localhost:61616");
ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory"); // Create a the JMS objects
// Step 3. Create a the JMS objects
connection = cf.createConnection(); connection = cf.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Perform the look-ups
Queue queue = session.createQueue("exampleQueue");
MessageConsumer messageConsumer = session.createConsumer(queue); MessageConsumer messageConsumer = session.createConsumer(queue);
MessageProducer producerAnswer = session.createProducer(queue); MessageProducer producerAnswer = session.createProducer(queue);
// Step 4. Start the connection // Start the connection
connection.start(); connection.start();
System.out.println("On a shell script, execute the following:"); System.out.println("On a shell script, execute the following:");
@ -71,18 +68,25 @@ public class ProtonCPPExample {
System.out.println("./hello"); System.out.println("./hello");
// Step 5. Finally, receive the message for (int i = 0; i < 10; i++) {
Message messageReceived = messageConsumer.receive(5000); try {
// Step 5. Finally, receive the message
TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);
if (messageReceived == null) { if (messageReceived == null) {
// We are not going to issue this as an error because System.out.println("No messages");
// we also use this example as part of our tests on artemis // We are not going to issue this as an error because
// this is not considered an error, just that no messages arrived (i.e. hello wasn't called) // we also use this example as part of our tests on artemis
} else { // this is not considered an error, just that no messages arrived (i.e. hello wasn't called)
System.out.println("message received: " + messageReceived); } else {
System.out.println("message received: " + messageReceived.getText());
// Sending message back to client // Sending message back to client
producerAnswer.send(session.createTextMessage("HELLO from Apache ActiveMQ Artemis")); producerAnswer.send(session.createTextMessage("HELLO from Apache ActiveMQ Artemis " + i + "!!"));
}
} catch (Throwable e) {
e.printStackTrace();
}
} }
} finally { } finally {
// Step 9. Be sure to close our resources! // Step 9. Be sure to close our resources!
@ -94,27 +98,4 @@ public class ProtonCPPExample {
} }
} }
} }
// To do this we send a management message to get the message count.
// In real life you wouldn't create a new session every time you send a management message
private int getMessageCount(final Connection connection) throws Exception {
QueueSession session = ((QueueConnection) connection).createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue managementQueue = ActiveMQJMSClient.createQueue("activemq.management");
QueueRequestor requestor = new QueueRequestor(session, managementQueue);
connection.start();
Message m = session.createMessage();
JMSManagementHelper.putAttribute(m, ResourceNames.QUEUE + "exampleQueue", "messageCount");
Message response = requestor.request(m);
int messageCount = (Integer) JMSManagementHelper.getResult(response);
return messageCount;
}
} }