Search in sources :

Example 41 with UtilException

use of cn.hutool.core.exceptions.UtilException in project hutool by looly.

the class StackTraceCaller method getCaller.

@Override
public Class<?> getCaller() {
    final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
    if (OFFSET + 1 >= stackTrace.length) {
        return null;
    }
    final String className = stackTrace[OFFSET + 1].getClassName();
    try {
        return Class.forName(className);
    } catch (ClassNotFoundException e) {
        throw new UtilException(e, "[{}] not found!", className);
    }
}
Also used : UtilException(cn.hutool.core.exceptions.UtilException)

Example 42 with UtilException

use of cn.hutool.core.exceptions.UtilException in project hutool by looly.

the class StackTraceCaller method getCaller.

@Override
public Class<?> getCaller(int depth) {
    final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
    if (OFFSET + depth >= stackTrace.length) {
        return null;
    }
    final String className = stackTrace[OFFSET + depth].getClassName();
    try {
        return Class.forName(className);
    } catch (ClassNotFoundException e) {
        throw new UtilException(e, "[{}] not found!", className);
    }
}
Also used : UtilException(cn.hutool.core.exceptions.UtilException)

Example 43 with UtilException

use of cn.hutool.core.exceptions.UtilException in project hutool by looly.

the class IdUtilTest method snowflakeBenchTest2.

@Test
@Ignore
public void snowflakeBenchTest2() {
    final Set<Long> set = new ConcurrentHashSet<>();
    // 线程数
    int threadCount = 100;
    // 每个线程生成的ID数
    final int idCountPerThread = 10000;
    final CountDownLatch latch = new CountDownLatch(threadCount);
    for (int i = 0; i < threadCount; i++) {
        ThreadUtil.execute(() -> {
            for (int i1 = 0; i1 < idCountPerThread; i1++) {
                long id = IdUtil.getSnowflake(1, 1).nextId();
                set.add(id);
            // Console.log("Add new id: {}", id);
            }
            latch.countDown();
        });
    }
    // 等待全部线程结束
    try {
        latch.await();
    } catch (InterruptedException e) {
        throw new UtilException(e);
    }
    Assert.assertEquals(threadCount * idCountPerThread, set.size());
}
Also used : ConcurrentHashSet(cn.hutool.core.collection.ConcurrentHashSet) UtilException(cn.hutool.core.exceptions.UtilException) CountDownLatch(java.util.concurrent.CountDownLatch) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 44 with UtilException

use of cn.hutool.core.exceptions.UtilException in project hutool by looly.

the class IdUtilTest method snowflakeBenchTest.

@Test
@Ignore
public void snowflakeBenchTest() {
    final Set<Long> set = new ConcurrentHashSet<>();
    final Snowflake snowflake = IdUtil.getSnowflake(1, 1);
    // 线程数
    int threadCount = 100;
    // 每个线程生成的ID数
    final int idCountPerThread = 10000;
    final CountDownLatch latch = new CountDownLatch(threadCount);
    for (int i = 0; i < threadCount; i++) {
        ThreadUtil.execute(() -> {
            for (int i1 = 0; i1 < idCountPerThread; i1++) {
                long id = snowflake.nextId();
                set.add(id);
            // Console.log("Add new id: {}", id);
            }
            latch.countDown();
        });
    }
    // 等待全部线程结束
    try {
        latch.await();
    } catch (InterruptedException e) {
        throw new UtilException(e);
    }
    Assert.assertEquals(threadCount * idCountPerThread, set.size());
}
Also used : ConcurrentHashSet(cn.hutool.core.collection.ConcurrentHashSet) UtilException(cn.hutool.core.exceptions.UtilException) Snowflake(cn.hutool.core.lang.Snowflake) CountDownLatch(java.util.concurrent.CountDownLatch) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 45 with UtilException

use of cn.hutool.core.exceptions.UtilException in project hutool by looly.

the class ServletUtil method write.

/**
 * 返回数据给客户端
 *
 * @param response    响应对象{@link HttpServletResponse}
 * @param text        返回的内容
 * @param contentType 返回的类型
 */
public static void write(HttpServletResponse response, String text, String contentType) {
    response.setContentType(contentType);
    Writer writer = null;
    try {
        writer = response.getWriter();
        writer.write(text);
        writer.flush();
    } catch (IOException e) {
        throw new UtilException(e);
    } finally {
        IoUtil.close(writer);
    }
}
Also used : UtilException(cn.hutool.core.exceptions.UtilException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer)

Aggregations

UtilException (cn.hutool.core.exceptions.UtilException)50 IOException (java.io.IOException)19 FastByteArrayOutputStream (cn.hutool.core.io.FastByteArrayOutputStream)5 IORuntimeException (cn.hutool.core.io.IORuntimeException)5 URL (java.net.URL)5 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 File (java.io.File)3 ObjectInputStream (java.io.ObjectInputStream)3 PrintWriter (java.io.PrintWriter)3 AccessibleObject (java.lang.reflect.AccessibleObject)3 Method (java.lang.reflect.Method)3 MalformedURLException (java.net.MalformedURLException)3 SocketException (java.net.SocketException)3 HashSet (java.util.HashSet)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 XPathExpressionException (javax.xml.xpath.XPathExpressionException)3 ConcurrentHashSet (cn.hutool.core.collection.ConcurrentHashSet)2 BufferedInputStream (java.io.BufferedInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2