Created
September 18, 2015 18:51
-
-
Save garcia-jj/a3dfa1f25ef0ef36cc11 to your computer and use it in GitHub Desktop.
JMS sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ot.jms; | |
import javax.ejb.ActivationConfigProperty; | |
import javax.ejb.MessageDriven; | |
import javax.jms.JMSException; | |
import javax.jms.Message; | |
import javax.jms.MessageListener; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
@MessageDriven(activationConfig = { | |
@ActivationConfigProperty(propertyName = "destinationLookup", propertyValue = "java:/global/jms/queue/printerQueue"), | |
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") | |
}) | |
public class PrinterReceiverAsync implements MessageListener { | |
private final transient Logger logger = LoggerFactory.getLogger(PrinterReceiverAsync.class); | |
@Override | |
public void onMessage(Message message) { | |
try { | |
String content = message.getBody(String.class); | |
logger.debug("Message received (async): {}", content); | |
} catch (JMSException e) { | |
logger.warn("An error occurs when process message", e); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ot.jms; | |
import javax.annotation.Resource; | |
import javax.ejb.Schedule; | |
import javax.ejb.Stateless; | |
import javax.inject.Inject; | |
import javax.jms.JMSContext; | |
import javax.jms.Queue; | |
@Stateless | |
public class PrinterSender { | |
@Inject | |
private JMSContext context; | |
@Resource(lookup = "java:/global/jms/queue/printerQueue") | |
private Queue queue; | |
@Schedule(second = "*/5", minute = "*", hour = "*") | |
public void fire() { | |
for (int i = 0; i < 100; i++) { | |
context.createProducer().send(queue, "the lazy dog jumps over the quick brown fox"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment