/demos/legacy/src/main/java/org/jboss/as/demos/ejb3/rar/SimpleQueueResourceAdapter.java

https://github.com/jocstar/jboss-as · Java · 125 lines · 88 code · 11 blank · 26 comment · 5 complexity · 69e1f969a11686728c7034d543bde193 MD5 · raw file

  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright (c) 2011, Red Hat, Inc., and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.demos.ejb3.rar;
  23. import javax.resource.ResourceException;
  24. import javax.resource.spi.ActivationSpec;
  25. import javax.resource.spi.BootstrapContext;
  26. import javax.resource.spi.Connector;
  27. import javax.resource.spi.ResourceAdapter;
  28. import javax.resource.spi.ResourceAdapterInternalException;
  29. import javax.resource.spi.TransactionSupport;
  30. import javax.resource.spi.UnavailableException;
  31. import javax.resource.spi.endpoint.MessageEndpoint;
  32. import javax.resource.spi.endpoint.MessageEndpointFactory;
  33. import javax.resource.spi.work.Work;
  34. import javax.resource.spi.work.WorkException;
  35. import javax.resource.spi.work.WorkManager;
  36. import javax.transaction.xa.XAResource;
  37. import java.util.LinkedList;
  38. import java.util.List;
  39. import java.util.concurrent.BlockingQueue;
  40. import java.util.concurrent.LinkedBlockingDeque;
  41. import static java.util.concurrent.TimeUnit.SECONDS;
  42. /**
  43. * @author <a href="mailto:cdewolf@redhat.com">Carlo de Wolf</a>
  44. */
  45. @Connector(
  46. reauthenticationSupport = false,
  47. transactionSupport = TransactionSupport.TransactionSupportLevel.NoTransaction)
  48. public class SimpleQueueResourceAdapter implements ResourceAdapter {
  49. private static final BlockingQueue<String> queue = new LinkedBlockingDeque<String>();
  50. private static WorkManager workManager;
  51. private static List<MessageEndpointFactory> endpointFactories = new LinkedList<MessageEndpointFactory>();
  52. @Override
  53. public void start(BootstrapContext ctx) throws ResourceAdapterInternalException {
  54. if (workManager != null)
  55. throw new ResourceAdapterInternalException("Can only start once");
  56. workManager = ctx.getWorkManager();
  57. }
  58. @Override
  59. public void stop() {
  60. workManager = null;
  61. }
  62. public static void deliver(String message) throws WorkException {
  63. queue.add(message);
  64. workManager.doWork(new Work() {
  65. @Override
  66. public void release() {
  67. Thread.currentThread().interrupt();
  68. }
  69. @Override
  70. public void run() {
  71. try {
  72. process();
  73. } catch (UnavailableException e) {
  74. throw new RuntimeException(e);
  75. }
  76. }
  77. });
  78. }
  79. @Override
  80. public void endpointActivation(MessageEndpointFactory endpointFactory, ActivationSpec spec) throws ResourceException {
  81. endpointFactories.add(endpointFactory);
  82. }
  83. @Override
  84. public void endpointDeactivation(MessageEndpointFactory endpointFactory, ActivationSpec spec) {
  85. endpointFactories.remove(endpointFactory);
  86. }
  87. @Override
  88. public XAResource[] getXAResources(ActivationSpec[] specs) throws ResourceException {
  89. // no crash recovery
  90. return null;
  91. }
  92. private static void process() throws UnavailableException {
  93. if (endpointFactories.size() == 0)
  94. return;
  95. MessageEndpoint endpoint = endpointFactories.get(0).createEndpoint(null);
  96. try {
  97. while (!queue.isEmpty()) {
  98. try {
  99. String message = queue.poll(30, SECONDS);
  100. try {
  101. ((PostmanPat) endpoint).deliver(message);
  102. } catch (Throwable t) {
  103. t.printStackTrace();
  104. // ignore
  105. }
  106. } catch (InterruptedException e) {
  107. return;
  108. }
  109. }
  110. } finally {
  111. endpoint.release();
  112. }
  113. }
  114. }