Skip to content

Commit

Permalink
Parameterize HelloWorldMessage example
Browse files Browse the repository at this point in the history
  • Loading branch information
pzygielo committed Jan 12, 2025
1 parent d1678e3 commit 715a1d3
Showing 1 changed file with 38 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2000, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024 Contributors to the Eclipse Foundation
* Copyright (c) 2024, 2025 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
Expand Down Expand Up @@ -35,6 +35,9 @@
import java.util.*;

public class HelloWorldMessage {
static final String QUEUE_NAME = System.getProperty("HelloWorldMessage.queueName", "world");
static final boolean SEND = Boolean.parseBoolean(System.getProperty("HelloWorldMessage.send", String.valueOf(true)));
static final boolean RECEIVE = Boolean.parseBoolean(System.getProperty("HelloWorldMessage.receive", String.valueOf(true)));

/**
* Main method.
Expand Down Expand Up @@ -112,50 +115,53 @@ public static void main(String[] args) {
//Instantiate a Destination
//administered object.
//This statement can be eliminated if the JNDI code above is used.
myQueue = new com.sun.messaging.Queue("world");
myQueue = new com.sun.messaging.Queue(QUEUE_NAME);


//Step 6:
//Create a message producer.
MessageProducer myMsgProducer = mySess.createProducer(myQueue);
if (SEND) {
//Step 6:
//Create a message producer.
MessageProducer myMsgProducer = mySess.createProducer(myQueue);


//Step 7:
//Create and send a message to the queue.
TextMessage myTextMsg = mySess.createTextMessage();
myTextMsg.setText("Hello World");
System.out.println("Sending Message: " + myTextMsg.getText());
myMsgProducer.send(myTextMsg);
//Step 7:
//Create and send a message to the queue.
TextMessage myTextMsg = mySess.createTextMessage();
myTextMsg.setText("Hello World");

System.out.println("Sending Message: " + myTextMsg.getText());
myMsgProducer.send(myTextMsg);
}

//Step 8:
//Create a message consumer.
MessageConsumer myMsgConsumer = mySess.createConsumer(myQueue);

if (RECEIVE) {
//Step 8:
//Create a message consumer.
MessageConsumer myMsgConsumer = mySess.createConsumer(myQueue);

//Step 9:
//Start the Connection created in step 3.
myConn.start();

//Step 9:
//Start the Connection created in step 3.
myConn.start();

//Step 10:
//Receive a message from the queue.
Message msg = myMsgConsumer.receive();

System.out.println("Received message has following properties set:");
for (Enumeration e = msg.getPropertyNames(); e.hasMoreElements(); ) {
Object propertyName = e.nextElement();
System.out.println("\t" + propertyName + "\t" + msg.getObjectProperty(propertyName.toString()));
}
//Step 10:
//Receive a message from the queue.
Message msg = myMsgConsumer.receive();

//Step 11:
//Retreive the contents of the message.
if (msg instanceof TextMessage) {
TextMessage txtMsg = (TextMessage) msg;
System.out.println("Read Message: " + txtMsg.getText());
}
System.out.println("Received message has following properties set:");
for (Enumeration e = msg.getPropertyNames(); e.hasMoreElements(); ) {
Object propertyName = e.nextElement();
System.out.println("\t" + propertyName + "\t" + msg.getObjectProperty(propertyName.toString()));
}


//Step 11:
//Retreive the contents of the message.
if (msg instanceof TextMessage) {
TextMessage txtMsg = (TextMessage) msg;
System.out.println("Read Message: " + txtMsg.getText());
}
}

}
}
Expand Down

0 comments on commit 715a1d3

Please sign in to comment.