예제 #1
0
 public void _sendOne(String topic, String msg) {
   Session session = null;
   try {
     session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
     Destination destination = session.createQueue(topic);
     MessageProducer producer = session.createProducer(destination);
     producer.setDeliveryMode(DeliveryMode.PERSISTENT);
     TextMessage message = session.createTextMessage(JSON.toJSONString(msg));
     producer.send(message);
     session.commit();
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     try {
       if (null != session) session.close();
     } catch (Throwable ignore) {
     }
   }
 }
예제 #2
0
  public final T execute() throws BasicException {

    if (s.isTransaction()) {
      return transact();
    } else {
      try {
        try {
          s.begin();
          T result = transact();
          s.commit();
          return result;
        } catch (BasicException e) {
          s.rollback();
          throw e;
        }
      } catch (SQLException eSQL) {
        throw new BasicException("Transaction error", eSQL);
      }
    }
  }
예제 #3
0
 public void sendOne(String topic, String msg) {
   long s = System.currentTimeMillis();
   Session session = null;
   try {
     session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
     Destination destination = session.createQueue(topic);
     MessageProducer producer = session.createProducer(destination);
     producer.setDeliveryMode(DeliveryMode.PERSISTENT);
     TextMessage message = session.createTextMessage(msg);
     producer.send(message);
     session.commit();
     logger.info("cost: {}ms {}/{}", System.currentTimeMillis() - s, topic, msg);
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     try {
       if (null != session) session.close();
     } catch (Throwable ignore) {
     }
   }
 }
예제 #4
0
  public static void main(String[] args) throws Exception {

    stopMQService();

    ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();

    Connection connection = connectionFactory.createConnection();
    connection.start();

    final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
    Destination destination = session.createQueue("my-queue");

    MessageConsumer consumer = session.createConsumer(destination);
    /*
     * //listener 方式 consumer.setMessageListener(new MessageListener() {
     *
     * public void onMessage(Message msg) { MapMessage message =
     * (MapMessage) msg; //TODO something.... System.out.println("收到消息:" +
     * new Date(message.getLong("count"))); session.commit(); }
     *
     * }); Thread.sleep(30000);
     */
    int i = 0;
    int num = 0;
    while (i < 3) {
      num++;
      MapMessage message = (MapMessage) consumer.receive();
      session.commit();
      // TODO something....
      // System.out.println("收到消息:" + new Date(message.getLong("count")));
      System.out.println("2收到第" + num + "条消息:" + message.getString("count"));

      //			System.out.println("收到第"+num+"条消息:" + message.getString("mes"));

    }

    session.close();
    connection.close();
  }
예제 #5
0
  private void initSession(Session session, ConnectionInfo ci) {
    boolean ignoreUnknownSetting = ci.getProperty("IGNORE_UNKNOWN_SETTINGS", false);
    String init = ci.getProperty("INIT", null);

    session.setAllowLiterals(true);
    CommandInterface command;
    for (String setting : ci.getKeys()) {
      if (SetTypes.contains(setting)) {
        String value = ci.getProperty(setting);
        try {
          command =
              session.prepareCommandLocal(
                  "SET " + session.getDatabase().quoteIdentifier(setting) + " " + value);
          command.executeUpdate();
        } catch (DbException e) {
          if (!ignoreUnknownSetting) {
            session.close();
            throw e;
          }
        }
      }
    }
    if (init != null) {
      try {
        command = session.prepareCommand(init, Integer.MAX_VALUE);
        command.executeUpdate();
      } catch (DbException e) {
        if (!ignoreUnknownSetting) {
          session.close();
          throw e;
        }
      }
    }
    session.setAllowLiterals(false);
    session.commit(true);
  }
예제 #6
0
  @Override
  public int run() throws Exception {
    // Default values of command line arguments
    String host = DEFAULT_HOST;
    int port = DEFAULT_PORT;
    String user = DEFAULT_USER;
    String pass = DEFAULT_PASS;
    String destination = DEFAULT_DESTINATION;
    String file = ""; // No default -- if not given, don't read/write file
    int sleep = 0;
    boolean showpercent = false;
    int batchSize = 0;
    int length = 500; // Length of message generated internally
    String properties = "";
    String format = "short";
    String durable = null;

    int n = 1; // n is the number of messages to process, or a specific
    //   message number, depending on content
    String url = ""; // No default -- if not given, don't use it

    String[] nonSwitchArgs = cl.getArgs();
    if (nonSwitchArgs.length > 0) n = Integer.parseInt(nonSwitchArgs[0]);

    String _destination = cl.getOptionValue("destination");
    if (_destination != null) destination = _destination;

    String _host = cl.getOptionValue("host");
    if (_host != null) host = _host;

    String _port = cl.getOptionValue("port");
    if (_port != null) port = Integer.parseInt(_port);

    String _file = cl.getOptionValue("file");
    if (_file != null) file = _file;

    String _user = cl.getOptionValue("user");
    if (_user != null) user = _user;

    String _pass = cl.getOptionValue("password");
    if (_pass != null) pass = _pass;

    String _url = cl.getOptionValue("url");
    if (_url != null) url = _url;

    String _sleep = cl.getOptionValue("sleep");
    if (_sleep != null) sleep = Integer.parseInt(_sleep);

    if (cl.hasOption("percent")) showpercent = true;

    String _batchSize = cl.getOptionValue("batch");
    if (_batchSize != null) batchSize = Integer.parseInt(_batchSize);

    String _L = cl.getOptionValue("length");
    if (_L != null) length = Integer.parseInt(_L);

    String _properties = cl.getOptionValue("properties");
    if (_properties != null) properties = _properties;

    String _durable = cl.getOptionValue("durable");
    if (_durable != null) durable = _durable;

    boolean batch = false;
    if (batchSize != 0) batch = true;

    String _format = cl.getOptionValue("format");
    if (_format != null) format = _format;

    ActiveMQConnectionFactory factory = getFactory(host, port, url);

    Connection connection = factory.createConnection(user, pass);

    if (durable != null) connection.setClientID(durable);

    connection.start();

    Session session = connection.createSession(batch, Session.AUTO_ACKNOWLEDGE);

    Topic topic = session.createTopic(destination);

    MessageConsumer consumer = null;
    if (durable != null) consumer = session.createDurableSubscriber(topic, "amqutil");
    else consumer = session.createConsumer(topic);

    int oldpercent = 0;
    for (int i = 0; i < n; i++) {
      javax.jms.Message message = consumer.receive();

      if (batch) if ((i + 1) % batchSize == 0) session.commit();

      if (sleep != 0) Thread.sleep(sleep);

      JMSUtil.outputMessage(format, message, file);

      if (showpercent) {
        int percent = i * 100 / n;
        if (percent != oldpercent) System.out.println("" + percent + "%");
        oldpercent = percent;
      }
    }

    if (batch) session.commit();

    connection.close();

    return 0;
  }