Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package familiajboss;
/**
*
* @author herberth
*
* Código com alguma repetição, mas funciona. Refatorar depois.
* Não se esqueça de adicionar no XML de configuração as seguintes linhas antes do </server>:
*
<mbean code="org.jboss.mq.server.jmx.Queue" name="jboss.messaging.destination:service=Queue,name=FamiliaJBoss">
<depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
</mbean>
<mbean code="org.jboss.mq.server.jmx.Queue" name="jboss.messaging.destination:service=Queue,name=PaiJBoss">
<depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
</mbean>
<mbean code="org.jboss.mq.server.jmx.Queue" name="jboss.messaging.destination:service=Queue,name=FilhoJBoss">
<depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
</mbean>
<mbean code="org.jboss.mq.server.jmx.Queue" name="jboss.messaging.destination:service=Queue,name=MaeJBoss">
<depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
</mbean>
<mbean code="org.jboss.mq.server.jmx.Queue" name="jboss.messaging.destination:service=Queue,name=FilhaJBoss">
<depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
</mbean>
<mbean code="org.jboss.mq.server.jmx.Queue" name="jboss.messaging.destination:service=Queue,name=TioJBoss">
<depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
</mbean>
<mbean code="org.jboss.mq.server.jmx.Queue" name="jboss.messaging.destination:service=Queue,name=SobrinhoJBoss">
<depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
</mbean>
*
*/
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.jms.ConnectionFactory;
import javax.jms.Connection;
import javax.jms.Session;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.TextMessage;
import javax.naming.NamingException;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Pai pai = new Pai();
Mae mae = new Mae();
Sobrinho sobrinho = new Sobrinho();
Tio tio = new Tio();
Filha filha = new Filha();
Filho filho = new Filho();
pai.leNoticia();
mae.leNoticia();
filho.recebeNoticia();
sobrinho.recebeNoticia();
}
}
class FamiliaBase
{
protected Connection con;
protected Destination destination;
protected Session session;
protected Context jndiContext;
public FamiliaBase(String queue)
{
System.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
System.setProperty("java.naming.factory.url.pkgs", "org.jnp.interfaces");
System.setProperty("java.naming.provider.url", "localhost");
try {
jndiContext = new InitialContext();
ConnectionFactory factory = (ConnectionFactory) jndiContext.lookup("ConnectionFactory");
con = factory.createConnection();
session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = (Destination) jndiContext.lookup(queue);
con.start();
}
catch(Exception e){
e.printStackTrace();
}
}
public void finish()
{
try{
session.close();
con.close();
jndiContext.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
class Pai extends FamiliaBase
{
public Pai()
{
super("queue/PaiJBoss");
try
{
MessageConsumer receiver = session.createConsumer(destination);
receiver.setMessageListener(new MessageListener() {
public void onMessage(Message msg) {
TextMessage text = (TextMessage) msg;
try {
System.out.println("Opa, nova mensagem pro pai: " + text.getText());
} catch (JMSException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}catch(Exception e){
e.printStackTrace();
}
}
public void leNoticia()
{
//avisa pra mãe que leu a noticia
try
{
Destination maeDestination = (Destination) jndiContext.lookup("queue/MaeJBoss");
MessageProducer maeSender = session.createProducer(maeDestination);
TextMessage message = session.createTextMessage("Estou mais esperto!");
maeSender.send(message);
}catch(Exception e){
e.printStackTrace();
}
}
}
class Filho extends FamiliaBase
{
public Filho()
{
super("queue/FilhoJBoss");
try
{
MessageConsumer receiver = session.createConsumer(destination);
receiver.setMessageListener(new MessageListener() {
public void onMessage(Message msg) {
TextMessage text = (TextMessage) msg;
try {
System.out.println("Opa, nova mensagem pro filho: " + text.getText());
} catch (JMSException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}catch(Exception e){
e.printStackTrace();
}
}
public void recebeNoticia()
{
//avisa pra irmã que tirará notas melhores na escola
try
{
Destination maeDestination = (Destination) jndiContext.lookup("queue/FilhaJBoss");
MessageProducer maeSender = session.createProducer(maeDestination);
TextMessage message = session.createTextMessage("Irei tirar melhores notas na escola!");
maeSender.send(message);
}catch(Exception e){
e.printStackTrace();
}
}
}
class Mae extends FamiliaBase
{
public Mae()
{
super("queue/MaeJBoss");
try{
MessageConsumer receiver = session.createConsumer(destination);
receiver.setMessageListener(new MessageListener() {
public void onMessage(Message msg) {
TextMessage text = (TextMessage) msg;
try {
System.out.println("Opa, nova mensagem pra mãe: " + text.getText());
} catch (JMSException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}catch(Exception e){
e.printStackTrace();
}
}
public void leNoticia()
{
//avisa pro pai que sera uma esposa melhor
try
{
Destination maeDestination = (Destination) jndiContext.lookup("queue/PaiJBoss");
MessageProducer maeSender = session.createProducer(maeDestination);
TextMessage message = session.createTextMessage("Serei uma esposa melhor!");
maeSender.send(message);
}catch(Exception e){
e.printStackTrace();
}
}
}
class Filha extends FamiliaBase
{
public Filha()
{
super("queue/FilhaJBoss");
try{
MessageConsumer receiver = session.createConsumer(destination);
receiver.setMessageListener(new MessageListener() {
public void onMessage(Message msg) {
TextMessage text = (TextMessage) msg;
try {
System.out.println("Opa, nova mensagem pra irmã: " + text.getText());
//notifica o irmão que vai deixar de ser patricinha
vouDeixarDeSerPatricinha();
} catch (JMSException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}catch(Exception e){
e.printStackTrace();
}
}
public void vouDeixarDeSerPatricinha()
{
try
{
Destination maeDestination = (Destination) jndiContext.lookup("queue/FilhoJBoss");
MessageProducer maeSender = session.createProducer(maeDestination);
TextMessage message = session.createTextMessage("Vou deixar de ser patricinha!");
maeSender.send(message);
}catch(Exception e){
e.printStackTrace();
}
}
}
class Sobrinho extends FamiliaBase
{
public Sobrinho()
{
super("queue/SobrinhoJBoss");
try{
MessageConsumer receiver = session.createConsumer(destination);
receiver.setMessageListener(new MessageListener() {
public void onMessage(Message msg) {
TextMessage text = (TextMessage) msg;
try {
System.out.println("Opa, nova mensagem pro sobrinho: " + text.getText());
} catch (JMSException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}catch(Exception e){
e.printStackTrace();
}
}
public void recebeNoticia()
{
try
{
Destination maeDestination = (Destination) jndiContext.lookup("queue/TioJBoss");
MessageProducer maeSender = session.createProducer(maeDestination);
TextMessage message = session.createTextMessage("Recebi a noticia, tio!");
maeSender.send(message);
}catch(Exception e){
e.printStackTrace();
}
}
}
class Tio extends FamiliaBase
{
public Tio()
{
super("queue/TioJBoss");
try{
MessageConsumer receiver = session.createConsumer(destination);
receiver.setMessageListener(new MessageListener() {
public void onMessage(Message msg) {
TextMessage text = (TextMessage) msg;
try {
System.out.println("Opa, nova mensagem pro tio: " + text.getText());
notificaSobrinho();
} catch (JMSException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}catch(Exception e){
e.printStackTrace();
}
}
public void notificaSobrinho()
{
try
{
Destination maeDestination = (Destination) jndiContext.lookup("queue/SobrinhoJBoss");
MessageProducer maeSender = session.createProducer(maeDestination);
TextMessage message = session.createTextMessage("Eu vou ser um tio melhor!");
maeSender.send(message);
}catch(Exception e){
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.